gpt4 book ai didi

node.js - 是否可以在 Apollo 模拟中传递参数?

转载 作者:行者123 更新时间:2023-12-05 03:28:35 25 4
gpt4 key购买 nike

在 Apollo docs它显示了这个例子:

const { ApolloServer, gql } = require('apollo-server');

const typeDefs = gql`
type Query {
hello: String
resolved: String
}
`;

const resolvers = {
Query: {
resolved: () => 'Resolved',
},
};

const mocks = {
Int: () => 6,
Float: () => 22.1,
String: () => 'Hello',
};

const server = new ApolloServer({
typeDefs,
resolvers,
mocks,
});

server.listen().then(({ url }) => {
console.log(`🚀 Server ready at ${url}`)
});

我希望能够传入一个参数,比如一个 ID,比如:

const mocks = {
Job: (id) => {
return somearray.filter(_id === id)
},
};

Apollo 可以吗?

最佳答案

在 Apollo 服务器 V3 上,您需要使用不同类型的代码。它与 V2 相比有一些重大变化。

您可以按照以下代码执行此操作:

import { buildClientSchema } from 'graphql';
import { addMocksToSchema } from '@graphql-tools/mock';
import { makeExecutableSchema } from '@graphql-tools/schema';

const schema = buildClientSchema(myGqlSchemaJson);


const executableSchema = makeExecutableSchema({
typeDefs: schema,
});

const schemaWithMocks = addMocksToSchema({
schema: executableSchema,
resolvers: {
Query: {
myCustomQuery: (_parent: any, args: any, teste: any) => {
if (args.url === '/stack-overflow') {
return {data: 'Cool website', url: args.url}
}
},
},
},
});


const server = new ApolloServer({
schema: schemaWithMocks,
})

关于node.js - 是否可以在 Apollo 模拟中传递参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71237748/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com