I am currently working with apollo federation service. I have a federated service linked to apollo gateway running on my localhost. This federated service wraps up a rest api which registers users using mutation call.
It also has a sample query which returns a wish field that says just ‘hi’.
When i run this service embedding into gateway, query and mutation calls are returning null data
Gateway.js
import { ApolloGateway, RemoteGraphQLDataSource } from ‘@apollo/gateway’;
import { ApolloServer } from ‘apollo-server’;
const port = 4000;
const gateway = new ApolloGateway({
serviceList: [{ name: 'User', url: 'http://localhost:8001/graphql' }],
buildService({ name, url }) {
return new RemoteGraphQLDataSource({
url,
didReceiveResponse({ response, request, context }) {
request.http && request.http.headers.set('first_name', context.user?.first_name);
request.http && request.http.headers.set('last_name', context.user?.last_name);
request.http && request.http.headers.set('email', context.user?.email);
request.http && request.http.headers.set('password', context.user?.password);
console.log(response, 'resp2');
return response;
},
});
},
});
(async () => {
const server = new ApolloServer({
gateway,
subscriptions: false,
context: ({ req }) => {
const first_name = req.headers.first_name || null;
const last_name = req.headers.last_name || null;
const email = req.headers.email || null;
const password = req.headers.password || null;
const user = {
first_name,
last_name,
email,
password,
};
return { user };
},
});
server.listen({ port: '2000' }).then(({ url }) => {
console.log(`Gateway is ready at ${url}`);
});
})();
user service index.js
import { ApolloServer, gql } from ‘apollo-server’;
import { buildFederatedSchema } from ‘@apollo/federation’;
import mutationResolvers from ‘./User/mutationResolvers/index.js’;
const typeDefs = gql`
type Error @key(fields: "status") @key(fields: "message") {
status: Int
message: String
}
input createUserInputData {
first_name: String!
last_name: String!
password: String!
email: String!
}
input createUserInput {
data: createUserInputData
}
type createUserResponse {
success: Boolean
error: Error
token: String
}
extend type Query {
wish: String
}
extend type Mutation {
createUser(request: createUserInput): createUserResponse
}
`;
const userResolvers = {
Query: {
wish: () => 'hi',
},
Mutation: {
...mutationResolvers,
},
};
const server = new ApolloServer({
schema: buildFederatedSchema([
{
typeDefs,
userResolvers,
},
]),
context: ({ req, res }) => {
console.log('headers', req.headers);
console.log(res, 'response');
return {
req,
res,
};
},
});
const port = ‘8001’;
server.listen({ port }).then(({ url }) => {
console.log(`Accounts service ready at ${url}`);
});
Mutation resolvers:
import axios from ‘axios’;
export default async (parent, args, contexts) => {
try {
debugger;
console.log(args, 'args');
const first_name = args.request.headers.first_name;
const email = args.request.headers.email;
const last_name = args.request.headers.last_name;
const password = args.request.headers.password;
const userInfo = {
first_name,
email,
last_name,
password,
};
console.log(first_name, 'name');
const response = await axios.post(
'http://localhost:5000/api/v1',
{ body: userInfo },
{
headers: {
'Content-Type': 'application/json',
},
},
);
return {
success: true,
error: null,
token: response.data.token,
};
} catch (e) {
console.log(e);
return {
success: false,
error: {
status: 500,
message: e,
},
};
}
};
Playground
query{
wish
}
response:
{
“data”: {
“wish”: null
}
}
It would be grateful if someone can help me what’s causing the null data instead of resolvers returned data