In the Apollo docs it shows this example:
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}`)
});
I want to be able to pass in an argument, such as an ID into it, like:
const mocks = {
Job: (id) => {
return somearray.filter(_id === id)
},
};
Is this possible with Apollo?
On Apollo server V3 you need to use a different kind of code. It has some breaking changes from V2.
You can do that by following the code below:
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,
})