apollo server QuickStart
QuickStart
リンク
インストール
$ yarn init -y
$ yarn add apollo-server graphql
$ yarn add typescript ts-node @types/node --dev
$ cat << EOF > tsconfig.json
{
"compilerOptions": {
"sourceMap": true,
"outDir": "dist",
"strict": true,
"lib": ["esnext"],
"esModuleInterop": true
}
}
EOF
ファイルの用意
// index.ts
import { ApolloServer, gql } from 'apollo-server'
const typeDefs = gql`
type Book {
title: String
author: String
}
type Query {
books: [Book]
}
`;
const books = [
{
title: 'Harry Potter and the Chamber of Secrets',
author: 'J.K. Rowling',
},
{
title: 'Jurassic Park',
author: 'Michael Crichton',
},
];
const resolvers = {
Query: {
books: () => books,
},
};
const server = new ApolloServer({ typeDefs, resolvers });
server.listen().then(({ url }) => {
console.log(`🚀 Server ready at ${url}`);
});
実行
$ yarn run ts-node index.ts
その他
index.ts 追加参考
$ yarn add graphql-type-json
$ yarn add @types/graphql-type-json --dev
import { ApolloServer, gql } from 'apollo-server'
import { addMockFunctionsToSchema, makeExecutableSchema } from 'apollo-server'
import { GraphQLScalarType } from 'graphql'
import { GraphQLJSON } from 'graphql-type-json'
const typeDefs = gql`
# Comments
scalar MyCustomScalar
scalar JSON
type Book {
title: String
author: Author
custom: MyCustomScalar
aField: JSON
}
type Author {
name: String
books: [Book]
}
type Post {
name: String
}
"Description for the type"
type MyObjectType {
"""
Description for field
Supports **multi-line** description for your [API](http://example.com)!
"""
myField: String!
otherField (
"Description for argument"
arg: Int
): String!
}
input PostAndMediaInput {
title: String
body: String
mediaUrls: [String]
}
input SimpleStringInput {
str: String
}
type Query {
books: [Book]
getBooks: [Book]
getAuthors: [Author]
getMyObjectTypes: [MyObjectType]
}
type Mutation {
addBook(title: String, author: String, value: MyCustomScalar, simple: SimpleStringInput): Book!
createPost(post: PostAndMediaInput): Post
}
`;
const books = [
{
title: 'Harry Potter and the Chamber of Secrets',
author: 'J.K. Rowling',
custom: 'custom value',
aField: {"jsonKey":"jsonvalue"},
},
{
title: 'Jurassic Park',
author: 'Michael Crichton',
custom: 'custom value',
aField: {"jsonKey":{"jsonKey2":"jsonvalue"}},
},
];
const resolvers = {
Query: {
books: () => books,
getBooks: () => books,
getAuthors: () => books.map(book => ({name: book.author})),
},
Mutation: {
addBook: ({values}: { values?: any[] }) => {
console.log(values?.[1])
return books[0]
}
},
JSON: GraphQLJSON,
MyCustomScalar: new GraphQLScalarType({
name: 'MyCustomScalar',
description: 'Description of my custom scalar type',
serialize(value) {
console.log("=====serialize=====")
console.log(value)
return value;
},
parseValue(value) {
console.log("=====parseValue=====")
return value;
},
parseLiteral(ast) {
console.log("=====parseLiteral=====")
console.log(ast.kind)
if ("value" in ast){
return ast.value
} else {
return "TODO"
}
}
}),
};
const schema = makeExecutableSchema({
typeDefs,
resolvers,
});
const server = new ApolloServer({
schema,
// playground: true, introspection: true,
// mocks: true,
});
addMockFunctionsToSchema({
schema,
mocks: {
MyCustomScalar: () => {
return "foo@bar.com"
}
},
preserveResolvers: true,
});
// The `listen` method launches a web server.
server.listen().then(({ url }) => {
console.log(`🚀 Server ready at ${url}`);
});