type User {
id: ID!
name: String!
email: String
age: Int
posts: [Post!]!
} Int # Integer
Float # Floating point
String # UTF-8 string
Boolean # true/false
ID # Unique identifier name: String! # Cannot be null tags: [String] # Nullable list of nullable strings
tags: [String!]! # Non-null list of non-null strings enum Status {
ACTIVE
INACTIVE
PENDING
} interface Node {
id: ID!
}
type User implements Node {
id: ID!
name: String!
} union SearchResult = User | Post | Comment input CreateUserInput {
name: String!
email: String!
} query {
users {
id
name
}
} query {
user(id: "1") {
id
name
email
}
} query GetUser($id: ID!) {
user(id: $id) {
id
name
}
} query {
firstUser: user(id: "1") {
name
}
secondUser: user(id: "2") {
name
}
} query {
user(id: "1") {
name
posts {
title
comments {
text
}
}
}
} mutation {
createUser(name: "John", email: "john@example.com") {
id
name
}
} mutation CreateUser($input: CreateUserInput!) {
createUser(input: $input) {
id
name
}
}
# Variables:
{
"input": {
"name": "John",
"email": "john@example.com"
}
} mutation UpdateUser($id: ID!, $name: String!) {
updateUser(id: $id, name: $name) {
id
name
}
} mutation DeleteUser($id: ID!) {
deleteUser(id: $id) {
success
message
}
} type Subscription {
messageCreated: Message
userOnline(userId: ID!): User
} subscription {
messageCreated {
id
content
sender {
name
}
}
} fragment UserFields on User {
id
name
email
} query {
user(id: "1") {
...UserFields
posts {
title
}
}
}
fragment UserFields on User {
id
name
email
} query {
search(text: "hello") {
... on User {
name
}
... on Post {
title
}
}
} query GetUser($withPosts: Boolean!) {
user(id: "1") {
name
posts @include(if: $withPosts) {
title
}
}
} query GetUser($skipEmail: Boolean!) {
user(id: "1") {
name
email @skip(if: $skipEmail)
}
} type User {
id: ID!
fullName: String @deprecated(reason: "Use name instead")
name: String!
} import { ApolloServer } from '@apollo/server';
import { startStandaloneServer } from '@apollo/server/standalone';
const typeDefs = `
type Query {
hello: String
}
`;
const resolvers = {
Query: {
hello: () => 'Hello World!'
}
};
const server = new ApolloServer({ typeDefs, resolvers });
const { url } = await startStandaloneServer(server);
console.log(`Server ready at ${url}`); const resolvers = {
Query: {
user: (parent, args, context) => {
return context.db.findUser(args.id);
},
users: (parent, args, context) => {
return context.db.findUsers();
}
},
Mutation: {
createUser: (parent, args, context) => {
return context.db.createUser(args.input);
}
},
User: {
posts: (parent, args, context) => {
return context.db.findPostsByUser(parent.id);
}
}
}; const server = new ApolloServer({ typeDefs, resolvers });
await startStandaloneServer(server, {
context: async ({ req }) => ({
db: database,
user: await getUser(req.headers.authorization)
})
}); import { ApolloClient, InMemoryCache, gql } from '@apollo/client';
const client = new ApolloClient({
uri: 'http://localhost:4000/graphql',
cache: new InMemoryCache()
}); const GET_USERS = gql`
query GetUsers {
users {
id
name
}
}
`;
const { data } = await client.query({ query: GET_USERS }); const CREATE_USER = gql`
mutation CreateUser($input: CreateUserInput!) {
createUser(input: $input) {
id
name
}
}
`;
await client.mutate({
mutation: CREATE_USER,
variables: { input: { name: "John" } }
}); import { useQuery, gql } from '@apollo/client';
const GET_USER = gql`
query GetUser($id: ID!) {
user(id: $id) {
name
}
}
`;
function User({ id }) {
const { loading, error, data } = useQuery(GET_USER, {
variables: { id }
});
if (loading) return <p>Loading...</p>;
if (error) return <p>Error: {error.message}</p>;
return <p>{data.user.name}</p>;
} import { useMutation, gql } from '@apollo/client';
const CREATE_USER = gql`...`;
function CreateUser() {
const [createUser, { loading, error }] = useMutation(CREATE_USER);
const handleSubmit = async () => {
await createUser({ variables: { input: { name } } });
};
} import { useSubscription, gql } from '@apollo/client';
const MESSAGE_SUBSCRIPTION = gql`
subscription OnMessageCreated {
messageCreated {
id
content
}
}
`;
function Messages() {
const { data } = useSubscription(MESSAGE_SUBSCRIPTION);
return <p>New: {data?.messageCreated.content}</p>;
}