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
cyril36
3
I give additional information just in case you want to use mocks with gateway federation and express
Here is my code
import { ApolloServer } from ‘@apollo/server’;
import { ApolloGateway } from ‘@apollo/gateway’;
import { readFileSync } from ‘fs’;
import { addMocksToSchema } from ‘@graphql-tools/mock’;
import bodyParser from ‘body-parser’;
const { json } = bodyParser;
import { mocks } from “./mocks.js”;
import { expressMiddleware } from ‘@apollo/server/express4’;
import express from ‘express’;
import http from ‘http’;
import cookieParser from ‘cookie-parser’
const supergraphSdl = readFileSync(‘./src/supergraph.graphql’).toString()
const gateway = new ApolloGateway({
supergraphSdl,
debug:true,
});
const {schema, executor} = await gateway.load();
const app = express();
const httpServer = http.createServer(app);
const server = new ApolloServer({
schema: addMocksToSchema({
schema: schema,
mocks : mocks,
}),
executor,
});
await server.start();
app.use(
‘/’,
cookieParser(),
json(),
expressMiddleware(server)
);
app.set(‘trust proxy’,true);
let port = 4000
await new Promise((resolve) => httpServer.listen({ port: port }, resolve));
console.log(🚀 Server ready at http://localhost:${port}/
);
I hope i can help