cyril36
1
I would like to mock my graphql subgraph at the gateway level.
Right now each subgraph mock its own service independently.
e.g of one subgraph :
const {buildSubgraphSchema} = require(‘@apollo/subgraph’);
const { addMocksToSchema } = require(‘@graphql-tools/mock’);
const server = new ApolloServer({
schema: addMocksToSchema({
schema: buildSubgraphSchema({ typeDefs, resolvers }),
}),
});
try {
const { url } = await startStandaloneServer(server);
console.log(🚀 Subgraph ${subgraphName} running at ${url}
);
} catch (err) {
console.error(err);
}
But to have a consistent data result across the supergraphql schema, i would need to have a similar above setup at the gateway level.
Is there any apolloGateway parameter that could mock my service such as :
const supergraphSdl = readFileSync(‘./src/supergraph.graphql’).toString()
const gateway = new ApolloGateway({
supergraphSdl,
mocks:true
});
const server = new ApolloServer({
gateway,
});
const { url } = await startStandaloneServer(server);
console.log(🚀 Server ready at ${url}
);
Thank you in advance for your help
cyril36
2
I have found a way of doing it
Below the code of my gateway index.js :
import { ApolloServer } from ‘@apollo/server’;
import { startStandaloneServer } from ‘@apollo/server/standalone’;
import { ApolloGateway } from ‘@apollo/gateway’;
import { readFileSync } from ‘fs’;
import { addMocksToSchema } from ‘@graphql-tools/mock’;
import { mocks } from “./mocks.js”;
const supergraphSdl = readFileSync(‘./src/supergraph.graphql’).toString()
const gateway = new ApolloGateway({
supergraphSdl,
});
const {schema, executor} = await gateway.load();
const server = new ApolloServer({
schema: addMocksToSchema({
schema: schema,
mocks : mocks,
}),
executor,
});
const { url } = await startStandaloneServer(server);
console.log(🚀 Server ready at ${url}
);
mocks.js :
import { faker } from “@faker-js/faker”;
export const mocks = {
Int: () => faker.datatype.number(100),
Float: () => faker.datatype.float({ min: 10, max: 100, precision: 0.001 }),
String: () => faker.random.words(),
Boolean: () => faker.datatype.boolean(),
};
If you want to use the default graphql mocking just replace the line
delete line 6 : import { mocks } from “./mocks.js”;
replace line 16 : mocks : mocks by mocks : true