GraphQL with Apollo Server and Client

Build full-stack GraphQL applications.

Implement GraphQL APIs with Apollo.

Server Setup

const { ApolloServer, gql } = require(‘apollo-server’);

const typeDefs = gql`

type Query {

users: [User]

user(id: ID!): User

}

type User {

id: ID!

name: String!

email: String!

}

`;

const resolvers = {

Query: {

users: () => users,

user: (_, { id }) => users.find(u => u.id === id)

}

};

Client Setup

import { ApolloClient, InMemoryCache, gql } from ‘@apollo/client’;

const client = new ApolloClient({

uri: ‘http://localhost:4000/graphql’,

cache: new InMemoryCache()

});

Query Example

const GET_USERS = gql`

query GetUsers {

users {

id

name

email

}

}

`;

Conclusion

GraphQL with Apollo enables flexible data fetching!

Leave a Comment