GraphQL is a query language for APIs and a runtime for executing those queries with your existing data. It provides a complete and understandable description of the data in your API, gives clients the power to ask for exactly what they need, makes it easier to evolve APIs over time, and enables powerful developer tools.
GraphQL uses SDL to define the structure of your data.
type User {
id: ID!
name: String!
email: String!
posts: [Post!]!
}
type Post {
id: ID!
title: String!
content: String!
author: User!
}
type Query {
user(id: ID!): User
users: [User!]!
post(id: ID!): Post
posts: [Post!]!
}
type Mutation {
createUser(name: String!, email: String!): User!
createPost(title: String!, content: String!, authorId: ID!): Post!
}
Basic Scalar Types:
Int: 32-bit integer
Float: Double-precision floating-point value
String: UTF-8 character sequence
Boolean: true or false
ID: Unique identifier
Object Types:
type User {
id: ID!
name: String!
email: String!
}
Input Types:
input UserInput {
name: String!
email: String!
}
Enum Types:
enum Role {
USER
ADMIN
MODERATOR
}
Interface Types:
interface Node {
id: ID!
}
type User implements Node {
id: ID!
name: String!
}
Union Types:
union SearchResult = User | Post | Comment
Basic Query:
query {
user(id: "123") {
name
email
posts {
title
}
}
}
Multiple Queries:
query {
user(id: "123") {
name
}
posts {
title
}
}
Aliases:
query {
user1: user(id: "123") {
name
}
user2: user(id: "456") {
name
}
}
Basic Mutation:
mutation {
createUser(name: "John Doe", email: "john@example.com") {
id
name
email
}
}
Multiple Mutations:
mutation {
createUser(name: "John Doe", email: "john@example.com") {
id
}
createPost(title: "Hello World", content: "This is my first post", authorId: "123") {
id
title
}
}
Basic Subscription:
subscription {
newPost {
id
title
author {
name
}
}
}
Example resolver in JavaScript:
const resolvers = {
Query: {
user: (parent, args, context, info) => {
return getUserById(args.id);
},
posts: () => getAllPosts(),
},
Mutation: {
createUser: (parent, args, context, info) => {
return createNewUser(args.name, args.email);
},
},
User: {
posts: (parent) => getPostsByUserId(parent.id),
},
};
Query with Variables:
query GetUser($userId: ID!) {
user(id: $userId) {
name
email
}
}
Variable Values:
{
"userId": "123"
}
Built-in Directives:
query GetUser($includeEmail: Boolean!) {
user(id: "123") {
name
email @include(if: $includeEmail)
}
}
Custom Directive:
directive @deprecated(reason: String) on FIELD_DEFINITION
type User {
id: ID!
name: String!
oldField: String @deprecated(reason: "Use newField instead")
newField: String
}
Fragment Definition and Usage:
fragment UserFields on User {
id
name
email
}
query {
user(id: "123") {
...UserFields
posts {
title
}
}
}
Introspection Query:
query {
__schema {
types {
name
kind
description
}
}
}
Context-based Authentication:
const server = new ApolloServer({
typeDefs,
resolvers,
context: ({ req }) => {
const token = req.headers.authorization || '';
const user = getUser(token);
return { user };
},
});
Resolver-level Authorization:
const resolvers = {
Query: {
sensitiveData: (parent, args, context) => {
if (!context.user) throw new Error('You must be logged in');
if (!context.user.hasPermission('READ_SENSITIVE_DATA')) {
throw new Error('Not authorized');
}
return getSensitiveData();
},
},
};
2024 © All rights reserved - buraxta.com