GRAND Stack queries always returns NULL - javascript

I am trying to setup a GRAND STACK project with a remote neo4j database, the following is the code for the very basic connection:
const { Neo4jGraphQL } = require("#neo4j/graphql");
const neo4j = require("neo4j-driver");
const { ApolloServer } = require("apollo-server");
const typeDefs = `
type Feature {
id: Int
name: String #cypher(statement: "MATCH (n:feature) RETURN n LIMIT 25")
}
type Symptom {
id: Int
name: String #cypher(statement: "MATCH (n:symptom) RETURN n LIMIT 25")
}
type Query {
features: [Feature],
symptoms: [Symptom]
}
`;
const driver = neo4j.driver(
"bolt://serverip:7687",
neo4j.auth.basic("neo4j", "hello-hello-hello")
);
const neoSchema = new Neo4jGraphQL({ typeDefs, driver });
// const resolvers = {
// Query: {
// feature: () => feature,
// },
// };
const server = new ApolloServer({
schema: neoSchema.schema,
context: ({ req }) => ({ req }),
// resolvers
});
server.listen(4000).then(() => console.log("Online"));
And I'm making the following query:
query Query {
symptoms {
name
}
}
The query always returns null. Any help would be greatly appreciated. Thanks.

Neo4j GraphQL autogenerates the queries and mutations for you. In your typeDefs remove the manual queries:
type Query {
features: [Feature]
symptoms: [Symptom]
}

Related

TypeError in Easygraphql-tester

I'm new to Graphql and Typescript.
My aim is to unit test Graphql resolvers with Easygraphql-tester. But I'm facing the following error.
TypeError: easygraphql_tester_1.EasyGraphQLTester is not a constructor
Following is my code:
import {EasyGraphQLTester} from "easygraphql-tester";
const schema = `
type FamilyInfo {
id: ID!
isLocal: Boolean!
}
type Query {
getFamilyInfoByIsLocal(isLocal: Boolean!): FamilyInfo
}
`
const query: any = `
query TEST($isLocal: Boolean!) {
getFamilyInfoByIsLocal(isLocal: $isLocal) {
id
isLocal
}
}
`
function getFamilyInfoByIsLocal(__, args, ctx) {
return {
id: 1,
isLocal: args.isLocal
}
}
const resolvers: any = {
Query: {
getFamilyInfoByIsLocal
}
}
const tester = new EasyGraphQLTester(schema, resolvers);
tester.graphql(query, undefined, undefined, { isLocal: false})
.then((result: any) => console.log(result))
.catch((err: any) => console.log(err));
Any idea why I'm facing the constructor error?
The problem lies is in way of importing. As there are no Type Definitions for this package. See Here
const EasyGraphQLTester = require('easygraphql-tester')

Inherit resolvers from graphql interface with Apollo Server v3

I would like to inherit resolvers from a graphql interface.
Consider this schema.
const typeDefs = gql`
interface Name {
name: String!
surname: String!
}
type Person implements Name {
_id: ID!
name: String!
surname: String!
}
type Query {
getPerson: Person!
}
}
And these resolvers:
const queryResolver = {
Name: {
name: () => "John",
surname: () => "Doe"
},
Query: {
getPerson: async (parent, args, context, info) => {
return {
_id: "1",
};
},
}
}
This is my server
const { ApolloServer } = require("apollo-server");
const typeDefs = require("./types");
const queryResolvers = require("./resolvers/query");
const resolvers = {
Query: queryResolvers.Query,
Name: queryResolvers.Name,
};
try {
const server = new ApolloServer({
typeDefs,
resolvers,
});
server.listen().then(({ url }) => {
console.log(`Apollo server listening on ${url}`);
});
} catch (e) {
console.error(e);
}
I would like that when querying the server
query Query {
getPerson {
name
surname
}
}
I get John Doe, as I would expect that Person inherits the resolvers from Name.
On ApolloServer v.2 I get this functionality implemented through inheritResolversFromInterfaces https://www.apollographql.com/docs/apollo-server/v2/api/graphql-tools/
I have not been able to find and equivalent on ApolloServer v3.0
The only option I could find is by creating the schema by using makeExecutableSchema from the #graphql-tools/schema, and then passing that schema to the ApolloServer constructor:
const schema = makeExecutableSchema({
typeDefs,
resolvers,
inheritResolversFromInterfaces: true,
});
const server = new ApolloServer({
...otherApolloArguments,
schema,
});

Apollo server : Mutation returns: Models.create(...) is not a function

I'm coding an Apollo server.
The Query work very well but when I'm trying to make a mutation it returns the Models.create(...).exec() is not a function. The new data I pass in gets created.
I've been trying to fix it for hours, and I don't know where I'm doing wrong
Here is my resolver
const { Models } = require('./dataSource/models')
const resolvers = {
Query: {
getPerson: async (_, args) => {
let response = await Models.find(args)
return response
}
},
Mutation: {
addPerson: async (_, args) => {
try{
let response = await Models.create(args).exec()
return response
}
catch(err){
return console.log(err)
}
}
}
}
module.exports = resolvers
and Here the model
const mongoose = require('mongoose')
const { Schema, model } = mongoose;
const PersonSchema = new Schema({
gender: String,
age: Number
});
const Models = model('person', PersonSchema)
module.exports = { Models }
Many thanks for your help

How can i test my resolvers properly with jest?

I'm trying to test my resolvers but i'd like to test each field of the response, here's the code to call the response:
interface Options {
source: string;
variableValues?: Maybe<{ [key: string]: unknown | null }>;
}
let schema: GraphQLSchema;
const gCall = async ({
source,
variableValues,
}: Options): Promise<ExecutionResult> => {
if (!schema) {
schema = await createSchema();
}
return graphql({
schema,
source,
variableValues,
});
};
export default gCall;
And that's the code to test the resolver:
let connection: Connection;
const challengeMutation = `
mutation CreateChallenge($data: CreateChallengeInput!) {
createChallenge(data: $data) {
id
name
category
startDate
endDate
goal
description
}
}
`;
describe('Create Challenge', () => {
beforeAll(async () => {
connection = await databaseTestConnection();
await connection.createQueryBuilder().delete().from(Challenge).execute();
});
afterAll(async () => {
await connection.createQueryBuilder().delete().from(Challenge).execute();
await connection.close();
});
it('should create challenge', async () => {
const challenge = {
name: 'some awesome name',
category: 'distância',
startDate: new Date(2020, 7, 4).toISOString(),
endDate: new Date(2020, 7, 5).toISOString(),
goal: 5000,
description: 'some excelent challenge description',
};
const response = await gCall({
source: challengeMutation,
variableValues: {
data: challenge,
},
});
expect(response).toMatchObject({
data: {
createChallenge: {
name: challenge.name,
category: challenge.category,
startDate: challenge.startDate,
endDate: challenge.endDate,
goal: challenge.goal,
description: challenge.description,
},
},
});
});
});
What I'd like to do is test the fields separately, like this:
expect(response.data.createChallenge.name).toEqual(challenge.name);
But I'm getting the following error when I try to execute the above code:
Object is possibly 'null' or 'undefined'.
What can I do to solve this error and to make this test better?
Object is possibly 'null' or 'undefined'.
TypeScript warns you that the response data might not exist as the graphql "server" might return error instead. So you should use ! operator to assert it's not null.
You should also do that after checking it's not undefined with expect().

How to use optimisticUpdater and updater in relay modern for create without edges?

how do I update the ui with a new item in relay modern?
I have all other CRUD working and but can't figure out the updater on commit mutation for a new item. When I refresh the page it there. All examples use viewer object which I don't have one in my schema.
schema.graphql
type Note {
id: String
avatar: String
message: String
date: String
}
create_mutation.js
export function createMutation({ mutation, variables, environment, create }) {
const fragment = mutation().fragment;
const operation = fragment.selections[0].name;
const mutationName = fragment.name;
const type = fragment.selections[0].concreteType;
const id = 'client:${operation}:${cuid()}';
const config = {
mutation,
variables,
optimisticUpdater: proxyStore => {
// 1. Create mock record that to be added to the store
const record = proxyStore.create(id, type);
Object.keys({ ...variables, id }).forEach(key =>
record.setValue(key, `${key}`)
);
const viewerProxy = proxyStore.get(operation);
const connection = ConnectionHandler.getConnection(record, mutationName);
if (connection) {
ConnectionHandler.insertEdgeAfter(viewerProxy, record);
}
},
updater: proxyStore => {},
onError: error => console.log(error)
};
commitMutation(environment, config);
}

Categories