Guest User

Untitled

a guest
Mar 10th, 2020
357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const { ApolloServer, gql } = require("apollo-server-lambda");
  2. const faunadb = require("faunadb");
  3. const q = faunadb.query;
  4.  
  5. var client = new faunadb.Client({secret: process.env.FAUNA})
  6.  
  7. // Construct a schema, using GraphQL schema language
  8. const typeDefs = gql`
  9.   type Query {
  10.     todos: [Todo]!
  11.   }
  12.   type Todo {
  13.     id: ID!
  14.     text: String!
  15.     done: Boolean!
  16.   }
  17.   type Mutation {
  18.     addTodo(text: String!): Todo
  19.     updateTodoDone(id: ID!): Todo
  20.   }
  21. `;
  22.  
  23. // Provide resolver functions for your schema fields
  24. const resolvers = {
  25.   Query: {
  26.     todos: async (parent, args, { user }) => {
  27.       if (!user) {
  28.         return [];
  29.       } else {
  30.         const results = await client.query(
  31.             q.Paginate(q.Match(q.Index("todos_by_user"), user))
  32.         );
  33.         return results.data.map(([ref, text, done]) => ({
  34.             id: ref.id,
  35.             text,
  36.             done
  37.         }));
  38.         //return [{id: "123", text: "foo", done: false}];
  39.       }
  40.     }
  41.   },
  42.   Mutation: {
  43.     addTodo: async (_, { text }, { user }) => {
  44.    
  45.       if (!user) {
  46.           throw new Error("Must be authenticated");
  47.       }
  48.  
  49.       const results = await client.query(
  50.           q.Create(q.Collection("todos"), {
  51.               data: {
  52.                   text,
  53.                   done: false,
  54.                   owner: user
  55.               }
  56.           })
  57.       )
  58.  
  59.       return {
  60.           ...results.data,
  61.           id: results.ref.id
  62.       };
  63.     },
  64.     updateTodoDone: async (_, { id }, { user }) => {
  65.  
  66.         if (!user) {
  67.             throw new Error("Must be authenticated");
  68.         }
  69.  
  70.       const results = await client.query(
  71.           q.Update(q.Ref(q.Collection("todos"), id), {
  72.               data: {
  73.                   done: true
  74.               }
  75.           }));
  76.       return {
  77.           ...results.data,
  78.           id: results.ref.id
  79.       }  
  80.      
  81.     }
  82.   }
  83. };
  84.  
  85. const server = new ApolloServer({
  86.   typeDefs,
  87.   resolvers,
  88.   context: ({ context }) => {
  89.     console.log(context.clientContext);
  90.     if (context.clientContext.user) {
  91.       return { user: context.clientContext.user.sub };
  92.     } else {
  93.       return {};
  94.     }
  95.   },
  96.   // By default, the GraphQL Playground interface and GraphQL introspection
  97.   // is disabled in "production" (i.e. when `process.env.NODE_ENV` is `production`).
  98.   //
  99.   // If you'd like to have GraphQL Playground and introspection enabled in production,
  100.   // the `playground` and `introspection` options must be set explicitly to `true`.
  101.   playground: true,
  102.   introspection: true
  103. });
  104.  
  105. exports.handler = server.createHandler({
  106.   cors: {
  107.     origin: "*",
  108.     credentials: true
  109.   }
  110. });
Advertisement
Add Comment
Please, Sign In to add comment