Apollo GraphQL-Import .graphql schema as typeDefs[英] Apollo GraphQL - Import .graphql schema as typeDefs

本文是小编为大家收集整理的关于Apollo GraphQL-Import .graphql schema as typeDefs的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

使用GraphQl-Yoga,您可以通过执行以下操作简单地导入模式:typeDefs: './src/schema.graphql'. Apollo-Server-Express是否有类似的方法?

如果没有,一个人如何从外部.graphql文件导入typedef?

推荐答案

我通过使用 graphql-import 这正是我需要的.请参阅下面的示例代码:

import { ApolloServer } from 'apollo-server-express'
import { importSchema } from 'graphql-import'
import Query from './resolvers/Query'

const typeDefs = importSchema('./src/schema.graphql')
const server = new ApolloServer({
    typeDefs,
    resolvers: {
        Query
    }
})

const app = express()
server.applyMiddleware({ app })

app.listen({ port: 4000 })

**

更新:graphql-import v0.7+

**

importSchema现在是异步的,应作为承诺处理.只需将其包装在async函数中,然后简单地将其包装在await中:

async function start() {
    const typeDefs = await importSchema(".src/schema.graphql")
}

其他推荐答案

您可以使用函数makeExecutableSchema传递typeDefs.这样的东西:

import { makeExecutableSchema } from 'graphql-tools';
import mySchema from './src/schema.graphql';

const app = express();

const schema = makeExecutableSchema({
  typeDefs: [mySchema],
  resolvers: {
    ...
  },
});

app.use(
  '/graphql',
  graphqlExpress({ schema })
);

其他推荐答案

作为最新的响应,遵循教程的顶部,此处链接可以将架构移动到一个名为schema.graphql的新文件,然后导入" fs"和" path"和" path"和" path"和输入,因此现在看起来像:

const fs = require('fs');
const path = require('path');

const server = new ApolloServer({
  typeDefs: fs.readFileSync(
    path.join(__dirname, 'schema.graphql'),
    'utf8'
  ),
  resolvers,
})

本文地址:https://www.itbaoku.cn/post/1938079.html

问题描述

With graphql-yoga you can simply import your schema by doing the following: typeDefs: './src/schema.graphql'. Is there a similar way of doing so with apollo-server-express?

If there isn't, how does one import the typeDefs from an external .graphql file?

推荐答案

I found a way of doing this by using graphql-import which does exactly what I need. See sample code below:

import { ApolloServer } from 'apollo-server-express'
import { importSchema } from 'graphql-import'
import Query from './resolvers/Query'

const typeDefs = importSchema('./src/schema.graphql')
const server = new ApolloServer({
    typeDefs,
    resolvers: {
        Query
    }
})

const app = express()
server.applyMiddleware({ app })

app.listen({ port: 4000 })

**

UPDATE: graphql-import v0.7+

**

importSchema is now async and should be handled as a promise. Just wrap it in a async function and simply await it:

async function start() {
    const typeDefs = await importSchema(".src/schema.graphql")
}

其他推荐答案

You can use the function makeExecutableSchema to pass in the typeDefs. Something like this:

import { makeExecutableSchema } from 'graphql-tools';
import mySchema from './src/schema.graphql';

const app = express();

const schema = makeExecutableSchema({
  typeDefs: [mySchema],
  resolvers: {
    ...
  },
});

app.use(
  '/graphql',
  graphqlExpress({ schema })
);

其他推荐答案

As a more recent response, following the top of the tutorial here link one can move the schema to a new file called schema.graphql and then import "fs" and "path" and input in the file so now it looks like:

const fs = require('fs');
const path = require('path');

const server = new ApolloServer({
  typeDefs: fs.readFileSync(
    path.join(__dirname, 'schema.graphql'),
    'utf8'
  ),
  resolvers,
})