How to use where clause in Knex
Create refunds with Stripe
Load HTML with Cheerio
How to send POST request with Axios
Find intersection of array in Lodash
Powered by Official white Bloop logo with a period

Terms / Privacy / Search / Support

  • const { ApolloServer } = require("apollo-server");
    const { ApolloServerPluginUsageReporting } = require("apollo-server-core");
    const server = new ApolloServer({
      typeDefs,
      resolvers,
      plugins: [
        ApolloServerPluginUsageReporting({
          rewriteError(err) {
            // Using a more stable, known error property (e.g. `err.code`) would be
            // more defensive, however checking the `message` might serve most needs!
            if (err.message && err.message.startsWith("Known error message")) {
              return null;
            }
    
            // All other errors should still be reported!
            return err;
          }
        }),
      ],
    });
    Docs
  • const {
      ApolloServer,
      gql,
      UserInputError
    } = require('apollo-server');
    
    const typeDefs = gql`
      type Query {
        userWithID(id: ID!): User
      }
    
      type User {
        id: ID!
        name: String!
      }
    `;
    
    const resolvers = {
      Query: {
        userWithID: (parent, args, context) => {
          if (args.id < 1) {
            throw new UserInputError('Invalid argument value');
          }
          // ...fetch correct user...
        },
      },
    };
    Docs
  • const { ApolloServer, gql, UserInputError } = require('apollo-server');
    const { GraphQLScalarType, Kind } = require('graphql');
    
    // Basic schema
    const typeDefs = gql`
      scalar Odd
    
      type Query {
        # Echoes the provided odd integer
        echoOdd(odd: Odd!): Odd!
      }
    `;
    
    // Validation function for checking "oddness"
    function oddValue(value) {
      if (typeof value === "number" && Number.isInteger(value) && value % 2 !== 0) {
        return value;
      }
      throw new UserInputError("Provided value is not an odd integer");
    }
    
    const resolvers = {
      Odd: new GraphQLScalarType({
        name: 'Odd',
        description: 'Odd custom scalar type',
        parseValue: oddValue,
        serialize: oddValue,
        parseLiteral(ast) {
          if (ast.kind === Kind.INT) {
            return oddValue(parseInt(ast.value, 10));
          }
          throw new UserInputError("Provided value is not an odd integer");
        },
      }),
      Query: {
        echoOdd(_, {odd}) {
          return odd;
        }
      }
    };
    
    const server = new ApolloServer({ typeDefs, resolvers });
    
    server.listen().then(({ url }) => {
      console.log(`🚀 Server ready at ${url}`)
    });
    Docs
  • const { ApolloServer, gql } = require('apollo-server');
    
    const libraries = [
      {
        branch: 'downtown'
      },
      {
        branch: 'riverside'
      },
    ];
    
    // The branch field of a book indicates which library has it in stock
    const books = [
      {
        title: 'The Awakening',
        author: 'Kate Chopin',
        branch: 'riverside'
      },
      {
        title: 'City of Glass',
        author: 'Paul Auster',
        branch: 'downtown'
      },
    ];
    
    // Schema definition
    const typeDefs = gql`
    
    # A library has a branch and books
      type Library {
        branch: String!
        books: [Book!]
      }
    
      # A book has a title and author
      type Book {
        title: String!
        author: Author!
      }
    
      # An author has a name
      type Author {
        name: String!
      }
    
      # Queries can fetch a list of libraries
      type Query {
        libraries: [Library]
      }
    `;
    
    // Resolver map
    const resolvers = {
      Query: {
        libraries() {
    
          // Return our hardcoded array of libraries
          return libraries;
        }
      },
      Library: {
        books(parent) {
    
          // Filter the hardcoded array of books to only include
          // books that are located at the correct branch
          return books.filter(book => book.branch === parent.branch);
        }
      },
      Book: {
    
        // The parent resolver (Library.books) returns an object with the
        // author's name in the "author" field. Return a JSON object containing
        // the name, because this field expects an object.
        author(parent) {
          return {
            name: parent.author
          };
        }
      }
    
      // Because Book.author returns an object with a "name" field,
      // Apollo Server's default resolver for Author.name will work.
      // We don't need to define one.
    };
    
    // Pass schema definition and resolvers to the
    // ApolloServer constructor
    const server = new ApolloServer({ typeDefs, resolvers });
    
    // Launch the server
    server.listen().then(({ url }) => {
      console.log(`🚀  Server ready at ${url}`);
    });
    Docs
  • Powered by Official black Bloop logo with a period
    download the IDE extension

    View other examples