我创建了这个问题,以防有人对如何在 Apollo 中添加联合/多态类型感到好奇。希望这会让他们更容易。
在此示例中,我希望响应为 aWorksheet
或ApiError
// typedefs.js
export default [`
schema {
query: Query
}
type Query {
worksheet(id: String!): Worksheet | Error
}
type Worksheet {
id: String!
name String
}
type ApiError {
code: String!
message: String!
}
`];
// resolvers.js
export default {
Query: {
worksheet(_, args, { loaders }) {
return loaders.worksheet.get(args.id).catch(() => {
// ApiError
return {
code: '1',
message: 'test'
}
});
}
}
};
// Express Server
import …
Run Code Online (Sandbox Code Playgroud) 我有一个带有 ApolloClient 和 Apollo-Link-Schema 的 React 应用程序。该应用程序在本地运行良好,但在我们的临时环境(使用 GOCD)中,我们收到以下错误:
Uncaught Error: Cannot use e "__Schema" from another module or realm.
Ensure that there is only one instance of "graphql" in the node_modules
directory. If different versions of "graphql" are the dependencies of other
relied on modules, use "resolutions" to ensure only one version is installed.
https://yarnpkg.com/en/docs/selective-version-resolutions
Duplicate "graphql" modules cannot be used at the same time since different
versions may have different capabilities and behavior. The data from one
version used …
Run Code Online (Sandbox Code Playgroud) graphql react-apollo apollo-client graphql-tools apollo-link
来自官方包文档:
GraphQLJSON 可以表示任何 JSON 可序列化的值,包括标量、数组和对象。GraphQLJSONObject 专门表示 JSON 对象,它涵盖了 JSON 标量的许多实际用例。
这听起来有点令人困惑,因为对我来说,这两个定义似乎非常相似。有人可以通过一个例子帮助我更好地理解这一点吗?谢谢期待。
我正在使用 Apollo-server-express 和 Graphql-tools。我已经翻遍了 Graphql-tools 文档,但我无法让它发挥作用。我试图让我的 schema.graphql 文件作为我的 typeDefs 导入。Graphql-tools 似乎应该使这变得容易,但有些事情并没有到位。
索引.js
const { ApolloServer } = require("apollo-server-express");
const { makeExecutableSchema } = require('@graphql-tools/schema');
const express = require("express");
const { join } = require("path");
const { loadSchema } = require("@graphql-tools/load");
const { GraphQLFileLoader } = require("@graphql-tools/graphql-file-loader");
const { addResolversToSchema } = require("@graphql-tools/schema");
const app = express();
const resolvers = {
Query: {
items: (parent, args, ctx, info) => {
return ctx.prisma.item.findMany();
},
},
Mutation: {
makeItem: (parent, args, context, info) …
Run Code Online (Sandbox Code Playgroud) 我使用 graphql-tools 缝合了两个 graphql 端点。使用一个端点架构它工作正常,但使用另一个架构它会抛出此错误“GraphQLError:在类型中找不到字段:'query_root'”。即使我可以在内省时看到所有模式。
const createRemoteExecutableSchemas = async () => {
let schemas = [];
for (let api of graphqlApis) {
const http = http_link.HttpLink({
uri: api.uri,
fetch:fetch
});
const link1 = setContext.setContext((request, previousContext) => {
return {
headers:{authorization: previousContext.graphqlContext.headers.authorization}
}
}).concat(http);
const link2 = setContext.setContext((request, previousContext) => ({
headers: {
'x-hasura-admin-secret': api.secret
}
})).concat(http);
const remoteSchema = await gtool.introspectSchema(link2);
const remoteExecutableSchema = gtool.makeRemoteExecutableSchema({
schema: remoteSchema,
link:link1
});
schemas.push(remoteExecutableSchema);
}
return schemas;
};
const createNewSchema = async () => …
Run Code Online (Sandbox Code Playgroud) graphql ×4
graphql-js ×2
react-apollo ×2
apollo ×1
apollo-link ×1
javascript ×1
json ×1
nodes ×1
schema ×1