getDirectives error when printing remote schema with graphql-tools - node.js

I have two node servers both serving graphql via express-graphql.
I wish to have my first server stitch its schema with that of the second server.
I have followed these instructions at: https://www.apollographql.com/docs/graphql-tools/remote-schemas
as I am using graphql-tools.
I am able to retrieve the schema from my second node server but as soon as I try to print it (with a mere console.log) i get this error:
Uncaught exception TypeError: schema.getDirectives is not a function
at printFilteredSchema (/Users/cchabert/git-repo/client-configuration-api/node_modules/graphql/utilities/schemaPrinter.js:61:27)
at Object.printSchema (/Users/cchabert/git-repo/client-configuration-api/node_modules/graphql/utilities/schemaPrinter.js:47:10)
at makeRemoteExecutableSchema (/Users/cchabert/git-repo/client-configuration-api/node_modules/graphql-tools/dist/stitching/makeRemoteExecutableSchema.js:60:30)
at schema (/Users/cchabert/git-repo/client-configuration-api/app/api/schema.js:68:24)
at module.exports (/Users/cchabert/git-repo/client-configuration-api/app/routes.js:102:13)
at Object.<anonymous> (/Users/cchabert/git-repo/client-configuration-api/app/index.js:15:20)
at Module._compile (internal/modules/cjs/loader.js:799:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:810:10)
at Module.load (internal/modules/cjs/loader.js:666:32)
at tryModuleLoad (internal/modules/cjs/loader.js:606:12)
at Function.Module._load (internal/modules/cjs/loader.js:598:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:862:12)
at internal/main/run_main_module.js:21:11
which causes the server to die but I am also able to see the schema somehow:
received schema: {"_queryType":"Query","_mutationType":"Mutation",
"_subscriptionType":null,"_directives":["#skip","#include","#deprecated"],
"_typeMap":{"Query":"Query","String":"String","Client":"Client",
"ID":"ID","DateTime":"DateTime",[...]}
I do see the _directives field in the remote schema definition but the docs isn't super clear on how to deal with this.
I have looked through the github issues of the graphql-tools repo as well but couldn't find anything.
Here is a code snippet:
const {
addMockFunctionsToSchema,
makeExecutableSchema,
makeRemoteExecutableSchema,
introspectSchema,
mergeSchemas
} = require('graphql-tools')
const _ = require('lodash)
const { createHttpLink } = require('apollo-link-http')
const fetch = require('node-fetch')
[..]
const customFetch = (uri, options = {}) => {
const httpOptions = _.merge(options, {
headers: {
'Content-type': 'application/json'
}
})
return fetch(uri, httpOptions)
}
function schema() {
const Query = `
type Query {
_empty: String
}
type Mutation {
_empty: String
}
`
const resolvers = {}
const mocks = {}
const localSchema = makeExecutableSchema({
typeDefs: [Query, [...]],
resolvers: [resolvers, [...]]
}) // by itself this schema works without any issues
const mergedMocks = _.merge(mocks, [...])
addMockFunctionsToSchema({
schema: localSchema,
mocks: mergedMocks,
preserveResolvers: true
})
const infoApiLink = createHttpLink({ uri, fetch: customFetch })
const remoteSchema = makeRemoteExecutableSchema({
schema: introspectSchema(infoApiLink).then(remoteSchema => {
console.log('received schema: ', JSON.stringify(remoteSchema))
return remoteSchema
}),
link: infoApiLink
})
return mergeSchemas({ schemas: [localSchema, remoteSchema] })
}
module.exports = {
schema
}
I would also like to make this work using only Promises (no async/await) as mentioned in https://github.com/apollographql/graphql-tools/blob/master/docs/source/remote-schemas.md#--introspectschemafetcher-context
Any suggestions welcome.

makeRemoteExecutableSchema should be passed an instance of a GraphQLSchema, but you're not doing that -- what you are doing is passing it a Promise that will resolve to a GraphQLSchema, which won't work. When you call introspectSchema, it has to make an introspection call, which is done asynchronously. It returns a Promise that resolves to the resulting GraphQLSchema object. We need to use await or then in order to get that value and then we can use it as needed.
The unnecessarily messy way without async/await:
function schema () {
// ... rest of your code
return introspectSchema(infoApiLink).then(schema => {
const remoteSchema = makeRemoteExecutableSchema({ schema, link: infoApiLink })
return mergeSchemas({ schemas: [localSchema, remoteSchema] })
})
}
Or using async/await:
async function schema () {
// ... rest of your code
const schema = await introspectSchema(infoApiLink)
const remoteSchema = makeRemoteExecutableSchema({ schema, link: infoApiLink })
return mergeSchemas({ schemas: [localSchema, remoteSchema] })
}
Either way, keep in mind by calling your schema function you will be returning a Promise that will resolve to the value returned by mergeSchemas. So where before you could have imported the function, called it and used the result directly, you will once again have to use either then or await to grab the value the Promise resolves to first:
import { schema } from './some-module'
schema()
.then(schema => {
const server = new ApolloServer({ schema })
server.listen()
})
.catch(error => {
// handle the Promise rejecting
})

Related

Next.js not build when using getStaticPaths and props

I'm trying to run next build when using getStaticProps and getStaticPaths method in one of my routes, but it fails every time. Firstly, it just couldn't connect to my API (which is obvious, they're created using Next.js' API routes which are not available when not running a Next.js app). I thought that maybe running a development server in the background would help. It did, but generated another problems, like these:
Error: Cannot find module for page: /reader/[id]
Error: Cannot find module for page: /
> Build error occurred
Error: Export encountered errors on following paths:
/
/reader/1
Dunno why. Here's the code of /reader/[id]:
const Reader = ({ reader }) => {
const router = useRouter();
return (
<Layout>
<pre>{JSON.stringify(reader, null, 2)}</pre>
</Layout>
);
};
export async function getStaticPaths() {
const response = await fetch("http://localhost:3000/api/readers");
const result: IReader[] = await response.json();
const paths = result.map((result) => ({
params: { id: result.id.toString() },
}));
return {
paths,
fallback: false,
};
}
export async function getStaticProps({ params }) {
const res = await fetch("http://localhost:3000/api/readers/" + params.id);
const result = await res.json();
return { props: { reader: result } };
}
export default Reader;
Nothing special. Code I literally rewritten from the docs and adapted for my site.
And here's the /api/readers/[id] handler.
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
const knex = getKnex();
const { id } = req.query;
switch (req.method) {
case "GET":
try {
const reader = await knex
.select("*")
.from("readers")
.where("id", id)
.first();
res.status(200).json(reader);
} catch {
res.status(500).end();
}
break;
}
}
Nothing special either. So why is it crashing every time I try to build my app? Thanks for any help in advance.
You should not fetch an internal API route from getStaticProps — instead, you can write the fetch code present in API route directly in getStaticProps.
https://nextjs.org/docs/basic-features/data-fetching#write-server-side-code-directly

Next.js - using BigQuery client library gives an error : Module not found: Can't resolve 'child_process'

I am trying to query bigQuery dataset from a next.js project.
I have installed #google-cloud/bigquery and followed the steps from here
I have also tried next.js related solutions from this link but still getting below error.
It looks like next.config.js needs to be configured for this to allow this api call. I am not sure what needs to be changed.
Could someone please help me resolve this issue?
here is my code :
const { BigQuery } = require("#google-cloud/bigquery");
const bigquery = new BigQuery();
useEffect(() => {
async function queryBigQuery() {
const query = `
SELECT fieldname
FROM \`db.dataset.tablename\` WHERE columnname = 50
LIMIT 10`;
const options = {
query: query,
};
// Run the query
const [rows] = await bigquery.query(options);
console.log("Query Results:");
rows.forEach((row) => {
const url = row["url"];
const viewCount = row["view_count"];
console.log(`url: ${url}, ${viewCount} views`);
});
}
queryBigQuery();
}, []);
**wait - compiling...
error - ./node_modules/google-auth-library/build/src/auth/googleauth.js:17:0
Module not found: Can't resolve 'child_process'**
UPDATED:
I am able to load bigQuery library I think on client side but its giving me new error.
Here is my latest next.config.js file
module.exports = {
webpack: (config, { isServer, webpack }) => {
if (!isServer) {
config.node = {
dgram: "empty",
fs: "empty",
net: "empty",
tls: "empty",
child_process: "empty",
};
}
return config;
},
env: {
project variables.
};
New Error:
#google-cloud/bigquery is meant to run on a Node.js environment, it won't work in the browser.
You'll need to move your code to a data fetching method like getStaticProps/getServerSideProps or to an API route, as they all run server-side.
Here's an example using an API route, as it seems to fit your use-case best.
// pages/api/bigquery
const { BigQuery } = require("#google-cloud/bigquery");
const bigquery = new BigQuery();
export default function handler(req, res) {
const query = `
SELECT fieldname
FROM \`db.dataset.tablename\` WHERE columnname = 50
LIMIT 10
`;
const options = {
query: query,
};
// Run your query/logic here
res.json(data); // Return your JSON data after logic has been applied
}
Then, in your React component's useEffect:
const queryBigQuery = async () => {
const res = await fetch('api/bigquery');
const data = await res.json(); // Returns JSON data from API route
console.log(data);
}
useEffect(() => {
queryBigQuery();
}, []);

GraphQL: Creating and Returning an Object in a Resolver?

I've got a mutation resolver that I call directly from the server like this:
import {graphql} from "graphql";
import {CRON_JOB_TO_FIND_USERS_WHO_HAVE_GONE_OFFLINE_MUTATION} from "../../query-library";
import AllResolvers from "../../resolvers";
import AllSchema from "../../schema";
import {makeExecutableSchema} from "graphql-tools";
const typeDefs = [AllSchema];
const resolvers = [AllResolvers];
const schema = makeExecutableSchema({
typeDefs,
resolvers
});
const {data, errors} = await graphql(
schema,
CRON_JOB_TO_FIND_USERS_WHO_HAVE_GONE_OFFLINE_MUTATION,
{},
{caller: 'synced-cron'},
{timeStarted: new Date().toISOString().slice(0, 19).replace('T', ' ')}
)
The mutation resolver is called and runs correctly. I don't need it to return anything, but GraphQL throws a warning if it doesn't, so I'd like it to return an object, any object.
So I'm trying it like this:
SCHEMA
cronJobToFindUsersWhoHaveGoneOffline(timeStarted: String): myUserData
QUERY
// note -- no gql. This string is passed directly to function graphql()
// where it gets gql applied to it.
const CRON_JOB_TO_FIND_USERS_WHO_HAVE_GONE_OFFLINE_MUTATION = `
mutation ($timeStarted: String){
cronJobToFindUsersWhoHaveGoneOffline(timeStarted: $timeStarted){
id,
},
}
`;
RESOLVER
cronJobToFindUsersWhoHaveGoneOffline(parent, args, context) {
return Promise.resolve()
.then(() => {
// there is code here that finds users who went offline if any
return usersWhoWentOffline;
})
.then((usersWhoWentOffline) => {
// HERE'S WHERE I HAVE TO RETURN SOMETHING FROM THE RESOLVER
let myUserDataPrototype = {
__typename: 'myUserData',
id: 'not_a_real_id'
}
const dataToReturn = Object.create(myUserDataPrototype);
dataToReturn.__typename = 'myUserData';
dataToReturn.id = 'not_a_real_id';
return dataToReturn; <==GRAPHQL IS NOT HAPPY HERE
})
.catch((err) => {
console.log(err);
});
},
}
GraphQL throws this warning:
data [Object: null prototype] {
cronJobToFindUsersWhoHaveGoneOffline: [Object: null prototype] { id: 'not_a_real_id' }
}
errors undefined
I have tried all kinds of different ways to fix this, but I haven't figured out the correct syntax yet.
What is a good way to handle this?
That doesn't appear to be a warning. That looks like you're writing the result to the console somewhere.

Need to find the error with connecting subscription with schema stitching

I am using apollo-server-express for graphql back-end. I am going to process only mutations there, but I want to redirect query and subscription on hasura by means of schema stitching with introspection. Queries through apollo-server to hasura are working fine and returning the expected data.
But subscriptions are not working and I am getting this error: " Expected Iterable, but did not find one for field subscription_root.users".
And besides, server hasura is receiving events:
But apollo-server resents the answer from hasura. It is not the first day I suffer with this and I can not understand what the problem is.
In the editor hasura subscriptions work.
Link to full code
If you need any additional info, I will gladly provide it to you.
import {
introspectSchema,
makeExecutableSchema,
makeRemoteExecutableSchema,
mergeSchemas,
transformSchema,
FilterRootFields
} from 'graphql-tools';
import { HttpLink } from 'apollo-link-http';
import nodeFetch from 'node-fetch';
import { resolvers } from './resolvers';
import { hasRoleResolver } from './directives';
import { typeDefs } from './types';
import { WebSocketLink } from 'apollo-link-ws';
import { split } from 'apollo-link';
import { getMainDefinition } from 'apollo-utilities';
import { SubscriptionClient } from 'subscriptions-transport-ws';
import * as ws from 'ws';
import { OperationTypeNode } from 'graphql';
interface IDefinitionsParams {
operation?: OperationTypeNode,
kind: 'OperationDefinition' | 'FragmentDefinition'
}
const wsurl = 'ws://graphql-engine:8080/v1alpha1/graphql';
const getWsClient = function (wsurl: string) {
const client = new SubscriptionClient(wsurl, {
reconnect: true,
lazy: true
}, ws);
return client;
};
const wsLink = new WebSocketLink(getWsClient(wsurl));
const createRemoteSchema = async () => {
const httpLink = new HttpLink({
uri: 'http://graphql-engine:8080/v1alpha1/graphql',
fetch: (nodeFetch as any)
});
const link = split(
({ query }) => {
const { kind, operation }: IDefinitionsParams = getMainDefinition(query);
console.log('kind = ', kind, 'operation = ', operation);
return kind === 'OperationDefinition' && operation === 'subscription';
},
wsLink,
httpLink,
);
const remoteSchema = await introspectSchema(link);
const remoteExecutableSchema = makeRemoteExecutableSchema({
link,
schema: remoteSchema
});
const renamedSchema = transformSchema(
remoteExecutableSchema,
[
new FilterRootFields((operation, fieldName) => {
return (operation === 'Mutation') ? false : true; // && fieldName === 'password'
})
]
);
return renamedSchema;
};
export const createNewSchema = async () => {
const hasuraExecutableSchema = await createRemoteSchema();
const apolloSchema = makeExecutableSchema({
typeDefs,
resolvers,
directiveResolvers: {
hasRole: hasRoleResolver
}
});
return mergeSchemas({
schemas: [
hasuraExecutableSchema,
apolloSchema
]
});
};
Fixed by installing graphql-tools 4th version. It tutns out the editor did not even notice that I do not have this dependency and simply took the version of node_modules, which was installed by some other package. Problem was with version 3.x. Pull request is where the bug was fixed.
I had the same problem, different cause and solution.
My subscription was working well, until I introduced the 'resolve' key in
my subscription resolver:
Here is the 'Subscription' part of My resolver:
Subscription: {
mySubName: {
resolve: (payload) => {
console.log('In mySubName resolver, payload:',payload)
return payload;
},
subscribe:() => pubSub.asyncIterator(['requestsIncomplete']),
// )
},
The console.log proved the resolve() function was being called with a well structured payload (shaped the same as my Schema definiton - specifically the an object with a key named after the graphQL Subscriber, pointing to an array (array is an iterable):
In mySubName resolver, payload: { mySubName:
[ { id: 41,
...,
},
{...},
{...}
...
...
]
Even though I was returning that same unadulterated object, it caused the error expected Iterable, but did not find one for field "Subscription.mySubName"
When I commented out that resolve function all together, the subscription worked, which is further evidence that my payload was well structured, with the right key pointing to an iterable.
I must be mis-using the resolve field. From https://www.apollographql.com/docs/graphql-subscriptions/subscriptions-to-schema/
When using subscribe field, it's also possible to manipulate the event
payload before running it through the GraphQL execution engine.
Add resolve method near your subscribe and change the payload as you wish
so I am not sure how to properly use that function, specifically don't know what shape object to return from it, but using it as above breaks the subscription in the same manner you describe in your question.
I was already using graphql-tools 4.0.0, I upgraded to 4.0.8 but it made no difference.

Mocking apollo graphql client

I have written the following node client which interacts with the graphql server and using apollo-tools node module. I was not able to find any mock test for the below in node. Pls let me know is there a way to mock the below piece of code.
const batchIdFetchClient = new ApolloClient({
uri: `http://localhost:3435`,
fetch,
})
await batchfetchclient.query({
query: gql`
query batcidId($batchid: String!) {
batchIds(batchid: $batchid){
batchrenewedId
}
}
`,
variables: {
batchid: 'exdsfsdfsfdid1234', // As of now hardcoded
},
})
.then(data => {
logger.info('BatchId Database Successful Response =>' + JSON.stringify(data))
})
.catch(error => {
logger.error('BatchId Database Error Response =>' + error)
})
Maybe you can try using easygraphql-tester, it'll be something like this:
You need to pass your schema in order to mock it
const EasyGraphQLTester = require('easygraphql-tester')
const tester = new EasyGraphQLTester(schema)
const query = gql`
query batcidId($batchid: String!) {
batchIds(batchid: $batchid){
batchrenewedId
}
}
`
const mock = tester.mock({
query,
variables: {
batchid: 'exdsfsdfsfdid1234', // As of now hardcoded
}
})
console.log(mock)
Also, you can set fixtures if you want to have a specific data.

Resources