0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

GraphQL サーバー構築(その3. GraphQLスキーマを外部ファイル化する)

Last updated at Posted at 2022-12-31

id: 72e2f090

前回

今回やること

  • GraphQLスキーマを外部ファイル化する
  • スキーマ読み込み

GraphQLスキーマを外部ファイル化する

  • .graphqlファイルを読み込むのに必要な依存をインストールします。
bash
$ yarn add @graphql-tools/graphql-file-loader @graphql-tools/load @graphql-tools/schema
  • src/schema.graphqlファイルを作成
schema.graphql
type Book {
  title: String
  author: String
}

type Query {
  books: [Book]
}

type Mutation {
  addBook(title: String, author: String): Book
}

スキーマ読み込み

index.ts
import { GraphQLFileLoader } from '@graphql-tools/graphql-file-loader';
import { loadSchemaSync } from '@graphql-tools/load';
import { addResolversToSchema } from '@graphql-tools/schema';

const schema = loadSchemaSync('src/schema.graphql', {
  loaders: [new GraphQLFileLoader()],
});

()

const schemaWithResolvers = addResolversToSchema({ schema, resolvers });
const server = new ApolloServer({ schema: schemaWithResolvers });

const { url } = await startStandaloneServer(server, {
  listen: { port: 4000 },
});

console.log(`🚀  Server ready at: ${url}`);

次回

  • GraphQLスキーマにもとづいたTypeScriptの型が自動生成

EOF
参考: GraphQLスキーマを外部ファイル化する

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?