前回の記事の内容に、リレーションの実装を追加します。前回の記事をご覧になっていない方はそちらを先に御覧ください。

Node.jsとExpressでGraphQLサーバーを作成!初心者向け手順解説②
前回の記事の内容に、スキーマとリゾルバーの定義、クエリとミューテーションの実装を追加します。前回の記事をご覧になっていない方はそちらを先に御覧ください。 const express = require('express'); const {...
この例では、ユーザーとポストのリレーションを扱います。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | const express = require( 'express' ); const { graphqlHTTP } = require( 'express-graphql' ); const { buildSchema } = require( 'graphql' ); // GraphQLスキーマの定義 const schema = buildSchema(` type User { id: Int! name: String! posts: [Post] } type Post { id: Int! title: String! content: String! author: User! } type Query { user(id: Int!): User post(id: Int!): Post users: [User] posts: [Post] } type Mutation { createUser(name: String!): User createPost(title: String!, content: String!, authorId: Int!): Post } `); // サンプルデータ const users = [ { id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }, { id: 3, name: 'Charlie' }, ]; const posts = [ { id: 1, title: 'GraphQL入門' , content: 'GraphQLは...' , authorId: 1 }, { id: 2, title: 'REST APIとの違い' , content: 'REST APIとは...' , authorId: 1 }, { id: 3, title: 'Expressでの実装' , content: 'Expressでの実装方法...' , authorId: 2 }, ]; // リゾルバ関数の定義 const root = { user: ({ id }) => users.find(user => user.id === id), post: ({ id }) => posts.find(post => post.id === id), users: () => users, posts: () => posts, createUser: ({ name }) => { const newUser = { id: users.length + 1, name }; users.push(newUser); return newUser; }, createPost: ({ title, content, authorId }) => { const newPost = { id: posts.length + 1, title, content, authorId }; posts.push(newPost); return newPost; }, }; // UserとPostのリレーション設定 schema.getType( 'User' ).getFields().posts.resolve = (user) => posts.filter(post => post.authorId === user.id); schema.getType( 'Post' ).getFields().author.resolve = (post) => users.find(user => user.id === post.authorId); // Expressサーバーの設定 const app = express(); app.use( '/graphql' , graphqlHTTP({ schema: schema, rootValue: root, graphiql: true , })); // サーバーの起動 |
このソースコードでは、User
と Post
の2つのタイプを定義し、それぞれのリレーションを設定しています。また、ユーザーとポストのクエリとミューテーションも追加しています。http://localhost:4000/graphql で実行できるいくつかのクエリとミューテーションの例を示します。
全てのユーザーを取得するクエリ
1 2 3 4 5 6 7 8 9 | { users { id name posts { title } } } |
全てのポストを取得するクエリ
1 2 3 4 5 6 7 8 9 10 | { posts { id title content author { name } } } |
特定のユーザーを取得するクエリ
1 2 3 4 5 6 7 8 9 | { user( id : 1) { id name posts { title } } } |
特定のポストを取得するクエリ
1 2 3 4 5 6 7 8 9 10 | { post( id : 1) { id title content author { name } } } |
新しいユーザーを作成するミューテーション
1 2 3 4 5 6 | mutation { createUser(name: "Dave" ) { id name } } |
新しいポストを作成するミューテーション
1 2 3 4 5 6 7 8 9 10 | mutation { createPost(title: "GraphQLのアドバンテージ" , content: "GraphQLのアドバンテージは..." , authorId: 1) { id title content author { name } } } |
これらのクエリとミューテーションを使って、ユーザーとポストのデータを取得したり、新しいデータを作成したりできます。この例をベースに、更に複雑なデータ構造やリゾルバを追加して、独自のGraphQLサーバーを構築してみてください。