My goal is to be able to pass an object as an argument in a GraphQL query.
Goal:
{
accounts (filter:
{"fieldName": "id",
"fieldValues":["123"],
"filterType":"in"}){
id
}
}
Error:
"message": "filterType fields must be an object with field names as keys or a function which returns such an object."
I've tried a few different approaches but this seems to be the closest to the potential solution.
Schema:
const filterType = new GraphQLObjectType ({
name: 'filterType',
fields: {
fieldName: { type: GraphQLString },
fieldValues: { type: GraphQLString },
filterType: { type: GraphQLString },
}
})
const QueryType = new GraphQLObjectType({
name: 'Query',
fields: () => ({
accounts: {
type: new GraphQLList(accountType),
args: {
filter: { type: new GraphQLInputObjectType(filterType) },
},
resolve: (root, args, { loaders }) => loaders.account.load(args),
},
}),
});
I have found the solution here.
https://github.com/mugli/learning-graphql/blob/master/7.%20Deep%20Dive%20into%20GraphQL%20Type%20System.md#graphqlinputobjecttype
Schema:
const filterType = new GraphQLInputObjectType({
name: 'filterType',
fields: {
fieldName: { type: GraphQLString },
fieldValues: { type: GraphQLString },
filterType: { type: GraphQLString },
}
})
const QueryType = new GraphQLObjectType({
name: 'Query',
fields: () => ({
accounts: {
type: new GraphQLList(accountType),
args: {
filter: { type: filterType },
},
resolve: (root, args, { loaders }) => {
return loaders.account.load(args)},
},
}),
});
Problem was in the query, I had the both the keys and values as strings in the object argument.
Correct Query:
{
accounts(filter: {fieldName: "id", fieldValues: "123", filterType: "in"}) {
id
}
}
You don't define filterType as an object type then wrap it in an input type, you literally create it as an input type:
const filterType = new GraphQLInputObjectType({
name: 'filterType',
fields: {
fieldName: { type: GraphQLString },
fieldValues: { type: GraphQLString },
filterType: { type: GraphQLString },
}
})
const QueryType = new GraphQLObjectType({
name: 'Query',
fields: () => ({
accounts: {
type: new GraphQLList(accountType),
args: {
filter: { type: filterType },
},
resolve: (root, args, { loaders }) => loaders.account.load(args),
},
}),
});
You'll also want to declare its type at query time, as illustrated in #piotrbienias's answer.
Related
I wanna set a default value in the role property but I don´t know how to do it.
The idea is that the role property is "BASIC" by default for all users.
I´m using express.
Sorry for my english, I´m not native, this is the code:
const UserType = new GraphQLObjectType({
name: "User",
description: "User type",
fields: () => ({
id: { type: GraphQLID },
username: { type: GraphQLString },
email: { type: GraphQLString },
displayName: { type: GraphQLString },
phone: { type: GraphQLString },
role: { type: GraphQLString}
}
),
});
thank you!
This is done with the defaultValue property. But this is not possible for the GraphQLObjectType as you show.
const UserType = new GraphQLObjectType({
name: 'User',
description: 'User type',
fields: () => ({
id: { type: GraphQLID },
username: { type: GraphQLString, defaultValue: 'default string' },
}),
});
Object literal may only specify known properties, and 'defaultValue' does not exist in type 'GraphQLFieldConfig<any, any, { [argName: string]: any; }>'
So GraphQLObjectType has no default Value property.
You need to solve this in a different place, not here. For example, when using data, if the value you want is empty, you can use default instead.
...
...
data.username ?? 'default string'
...
...
But where does this defaultValue property work? It works with GraphQLInputObjectType.
For example:
const filter = new GraphQLInputObjectType({
name: "Filter",
fields: () => ({
min: { type: new GraphQLNonNull(graphql.GraphQLInt) },
max: { type: graphql.GraphQLBoolean, defaultValue: 100 },
}),
});
and we can use it like this:
...
query: {
products: {
type: new GraphQLList(productTypes),
args: { filter: { type: new GraphQLNonNull(filter) } }, // <-----
resolve: allProducts,
},
},
...
This is already answered on https://stackoverflow.com/a/51567429/10310278
And official documentation https://graphql.org/graphql-js/type/#example-5
Please check these things out.
I've tried to isolate this example and I hope it's ok. I know, this isn't great code, but I hope you get the drift.
For the time being the resolvers return a static result object.
Here's my problem:
The result of the company resolve function should be passed on the user's resolve function. But that ain't happenin' and I wonder what I am missing.
const GraphQL = require('graphql');
const UserType = new GraphQL.GraphQLObjectType({
name: 'User',
fields: {
givenName: { type: GraphQL.GraphQLString },
familyName: { type: GraphQL.GraphQLString },
city: { type: GraphQL.GraphQLString },
},
});
const CompanyType = new GraphQL.GraphQLObjectType({
name: 'Company',
fields: {
legalName: { type: GraphQL.GraphQLString },
city: { type: GraphQL.GraphQLString },
employees: { type: new GraphQL.GraphQLList(UserType) },
},
});
const queryDef = new GraphQL.GraphQLObjectType({
name: 'Query',
fields: {
user: {
type: UserType,
args: {
id: { type: GraphQL.GraphQLID },
givenName: { type: GraphQL.GraphQLString },
familyName: { type: GraphQL.GraphQLString },
city: { type: GraphQL.GraphQLString },
},
resolve: (parent, args, context, info) => {
console.log('parent should provide company object', parent);
// currentyl parent is undefined
return {
id: 10,
givenName: 'test',
};
},
},
company: {
type: CompanyType,
args: {
id: { type: GraphQL.GraphQLID },
},
resolve: (parent, args, context, info) => {
return {
id: 3,
legalName: 'legal test name',
city: 'company location',
};
},
},
},
});
const schema = new GraphQL.GraphQLSchema({ query: queryDef });
const companyQuery = `
{
company(id: 1) {
city
employees {
familyName
}
}
}`;
GraphQL.graphql(schema, companyQuery).then( (companyResult) => {
console.log(companyResult);
} ).catch( (err) => {
console.error(err);
});
I'm trying to separate out my types into individual files. By doing so I've found that any type definition that references the usertype I get the below error.
User:
const {
GraphQLObjectType,
GraphQLList,
GraphQLString,
GraphQLBoolean,
GraphQLID,
} = require("graphql");
const UserType = new GraphQLObjectType({
name: "User",
fields: () => ({
_id: { type: GraphQLID },
name: { type: GraphQLString },
password: { type: GraphQLString },
email: { type: GraphQLString },
active: { type: GraphQLBoolean },
emailConfirmed: { type: GraphQLBoolean },
licenses: {
type: new GraphQLList(LicenseType),
async resolve(parent, args) {
return await LicenseController.getUserLicenses({ id: parent._id });
},
},
services: {
type: new GraphQLList(ServiceType),
async resolve(parent, args) {
return await ServiceController.getUserServices({ id: parent._id });
},
},
}),
});
const LicenseType = require("../types/licenseType");
const ServiceType = require("../types/serviceType");
const LicenseController = require("../../controllers/licenseController");
const ServiceController = require("../../controllers/serviceController");
module.exports = UserType;
License:
const {
GraphQLObjectType,
GraphQLInt,
GraphQLString,
GraphQLBoolean,
GraphQLID,
} = require("graphql");
const UserType = require("../types/userType");
const UserController = require("../../controllers/userController");
const LicenseType = new GraphQLObjectType({
name: "License",
fields: () => ({
_id: { type: GraphQLID },
token: { type: GraphQLString },
creationDate: { type: GraphQLString },
expirationDate: { type: GraphQLString },
btcAddress: { type: GraphQLString },
sessions: { type: GraphQLInt },
active: { type: GraphQLBoolean },
user_id: { type: GraphQLID },
user: {
type: UserType,
async resolve(parent, args) {
return await UserController.getSingleUser({ id: parent.user_id });
},
},
}),
});
module.exports = LicenseType;
Error:
Error: One of the provided types for building the Schema is missing a name.
I've tried moving the type/controller definitions above and below the type definitions with no change. How would I provide the user data from the license type?
You can move the require calls inside the fields function -- this way they will not be evaluated until the function is actually called when your schema is constructed.
const LicenseType = new GraphQLObjectType({
name: "License",
fields: () => {
const UserType = require("../types/userType");
const UserController = require("../../controllers/userController");
return {
_id: { type: GraphQLID },
token: { type: GraphQLString },
creationDate: { type: GraphQLString },
expirationDate: { type: GraphQLString },
btcAddress: { type: GraphQLString },
sessions: { type: GraphQLInt },
active: { type: GraphQLBoolean },
user_id: { type: GraphQLID },
user: {
type: UserType,
async resolve(parent, args) {
return await UserController.getSingleUser({ id: parent.user_id });
},
},
}
},
});
I am using graphql in node js for my oracle database wherein I connect to the remote database and fetch some details. I am fairly new to these technologies so please pardon me. I have a customer table with below schema:
const Customer = new GraphQLObjectType({
description: 'Customer data schema',
name: 'Customer',
fields: () => ({
name: {
type: GraphQLString,
sqlColumn: 'NAME',
},
city: {
type: GraphQLString,
sqlColumn: 'CITY'
},
country: {
type: GraphQLString,
sqlColumn: 'COUNTRY'
},
gender: {
type: GraphQLString,
sqlColumn: 'GENDER'
},
emp_id: {
type: GraphQLString,
sqlColumn: 'EMP_ID'
}
})
});
Customer._typeConfig = {
sqlTable: 'CUSTOMER',
uniqueKey: ['NAME','EMP_ID']
}
Using join monster I create my Query root as:
const QueryRoot = new GraphQLObjectType({
description: 'global query object',
name: 'RootQuery',
fields: () => ({
customer: {
type: new GraphQLList(Customer),
args: {
emp_id: {
description: 'Emp Id',
type: GraphQLString
},
name: {
description: 'Customer Name',
type: GraphQLString
}
},
where: (customer, args, context) => {
return `${customer}."EMP_ID" = :emp_id AND ${customer}."NAME" = :name`;
},
resolve: (parent, args, context, resolveInfo) => {
return joinMonster(resolveInfo, context, sql => {
console.log('joinMaster', sql);
return database.simpleExecute(sql, args,{
outFormat: database.OBJECT
});
});
}
}
})
})
When I pass my query in graphql in browser with emp_id and name parameters I get data. But there are cases when I cannot pass any parameters and would want all the rows to be fetched.
When I do not send the parameters I get error as:
ORA-01008 : Not all variables bound
I want the arguments to be optional, and if I don't send them then it should return all rows.
Thank you.
Both the where and resolver functions are passed an args argument. This will have the parameter names and values if any. You can use that argument to build a dynamic where clause. Here's an untested example:
const QueryRoot = new GraphQLObjectType({
description: 'global query object',
name: 'RootQuery',
fields: () => ({
customer: {
type: new GraphQLList(Customer),
args: {
emp_id: {
description: 'Emp Id',
type: GraphQLString
},
name: {
description: 'Customer Name',
type: GraphQLString
}
},
where: (customer, args, context) => {
if (Object.keys(args).length === 0) {
return false;
}
let whereClause = '1 = 1';
if (args.emp_id != undefined) {
whereClause += `\n AND ${customer}."EMP_ID" = :emp_id`;
}
if (args.name != undefined) {
whereClause += `\n AND ${customer}."NAME" = :name`;
}
return whereClause;
},
resolve: (parent, args, context, resolveInfo) => {
return joinMonster(resolveInfo, context, sql => {
console.log('joinMaster', sql);
return database.simpleExecute(sql, args,{
outFormat: database.OBJECT
});
});
}
}
})
})
Since the where clause would then match the number of arguments, you shouldn't get the ORA-01008 error.
I was hosting my data using json server on localhost:3000(REST api) and using GraphQL to fetch data, and now I'd like to move data into a Elastic Search server, and I still want to use GraphQL as API gateway. I tried this graphql-compose-elasticsearch library to use GraphQL as a proxy for ElasticSearch.
https://github.com/graphql-compose/graphql-compose-elasticsearch
In my original GraphQL schema, I defined types, root query, resolvers. The code is like this:
const graphql = require('graphql');
const axios = require('axios');
const {
GraphQLObjectType,
GraphQLList,
GraphQLID,
GraphQLInt,
GraphQLString,
GraphQLBoolean,
GraphQLSchema,
GraphQLNonNull
} = graphql;
const RoomType = new GraphQLObjectType({
name: 'RoomType',
fields: () => ({
id: { type: GraphQLID },
roomName: { type: GraphQLString },
roomNumber: { type: GraphQLString },
floorId: { type: GraphQLInt },
hasImages: { type: GraphQLBoolean },
hasGLTF: { type: GraphQLBoolean },
hasPhotogrammetry: { type: GraphQLBoolean },
hasPointClouds: { type: GraphQLBoolean },
roomDescription: { type: GraphQLString },
floor: {
type: FloorType,
resolve(parentValue, args) {
return axios
.get(`http://localhost:3000/floors/${parentValue.floorId}`)
.then((resp) => resp.data);
}
},
assets: {
type: new GraphQLList(AssetType),
resolve(parentValue, args) {
return axios
.get(`http://localhost:3000/rooms/${parentValue.id}/assets`)
.then((resp) => resp.data);
}
}
})
});
const FloorType = new GraphQLObjectType({
name: 'FloorType',
fields: () => ({
id: { type: GraphQLID },
floorName: { type: GraphQLString },
floorDescription: { type: GraphQLString },
rooms: {
type: new GraphQLList(RoomType),
resolve(parentValue, args) {
return axios
.get(`http://localhost:3000/floors/${parentValue.id}/rooms`)
.then((resp) => resp.data);
}
}
})
});
const AssetType = new GraphQLObjectType({
name: 'AssetType',
fields: () => ({
id: { type: GraphQLID },
category: { type: GraphQLString },
assetName: { type: GraphQLString },
assetNumber: { type: GraphQLString },
roomId: { type: GraphQLString },
location: { type: GraphQLString },
isHeritageAsset: { type: GraphQLBoolean },
hasImages: { type: GraphQLBoolean },
hasGLTF: { type: GraphQLBoolean },
hasPhotogrammetry: { type: GraphQLBoolean },
hasPointClouds: { type: GraphQLBoolean },
assetDescription: { type: GraphQLString },
room: {
type: RoomType,
resolve(parentValue, args) {
return axios
.get(`http://localhost:3000/rooms/${parentValue.roomId}`)
.then((resp) => resp.data);
}
}
})
});
const RootQuery = new GraphQLObjectType({
name: 'RootQueryType',
fields: {
getRoom: {
type: RoomType,
args: { id: { type: new GraphQLNonNull(GraphQLID) } },
resolve(parentValue, { id }) {
return axios.get(`http://localhost:3000/rooms/${id}`).then((resp) => resp.data);
}
},
getFloor: {
type: FloorType,
args: { id: { type: new GraphQLNonNull(GraphQLID) } },
resolve(parentValue, { id }) {
return axios.get(`http://localhost:3000/floors/${id}`).then((resp) => resp.data); //to make it compatible between axios and graphql, a workaround
}
},
getAsset: {
type: AssetType,
args: { id: { type: new GraphQLNonNull(GraphQLID) } },
resolve(parentValue, { id }) {
return axios.get(`http://localhost:3000/assets/${id}`).then((resp) => resp.data); //to make it compatible between axios and graphql, a workaround
}
},
getAllRooms: {
type: new GraphQLList(RoomType),
resolve() {
return axios.get(`http://localhost:3000/rooms`).then((resp) => resp.data);
}
},
getAllAssets: {
type: new GraphQLList(AssetType),
resolve() {
return axios.get(`http://localhost:3000/assets`).then((resp) => resp.data);
}
},
getAllFloors: {
type: new GraphQLList(FloorType),
resolve() {
return axios.get(`http://localhost:3000/floors`).then((resp) => resp.data);
}
}
}
});
//expose this to the rest of the application
module.exports = new GraphQLSchema({
query: RootQuery
});
For Elasticsearch, the version is 7.0. I'm running on localhost:9200, and I have 3 indices, rooms, floors, and assets, and each of the index have a mapping. I tried to code like this:
const graphql = require('graphql');
const graphql_compose_elasticsearch = require('graphql-compose-elasticsearch');
const { elasticApiFieldConfig, composeWithElastic } = graphql_compose_elasticsearch;
const { GraphQLSchema, GraphQLObjectType } = graphql;
const elasticsearch = require('elasticsearch');
// Mapping obtained from ElasticSearch server
const floor_mapping = {
properties: {
floorId: {
type: 'text',
fields: {
keyword: {
type: 'keyword',
ignore_above: 256
}
}
},
hasGLTF: {
type: 'boolean'
},
hasImages: {
type: 'boolean'
},
hasPhotogrammetry: {
type: 'boolean'
},
hasPointClouds: {
type: 'boolean'
},
roomDescription: {
type: 'text',
fields: {
keyword: {
type: 'keyword',
ignore_above: 256
}
}
},
roomName: {
type: 'text',
fields: {
keyword: {
type: 'keyword',
ignore_above: 256
}
}
},
roomNumber: {
type: 'text',
fields: {
keyword: {
type: 'keyword',
ignore_above: 256
}
}
}
}
};
const room_mapping = {
//similar
};
const asset_mapping = {
//similar
};
const Room = composeWithElastic({
graphqlTypeName: 'RoomType',
elasticIndex: 'rooms',
elasticType: '_doc',
elasticMapping: room_mapping,
elasticClient: new elasticsearch.Client({
host: 'http://localhost:9200',
apiVersion: '7.0'
})
});
const Floor = composeWithElastic({
graphqlTypeName: 'FloorType',
elasticIndex: 'floors',
elasticType: '_doc',
elasticMapping: floor_mapping,
elasticClient: new elasticsearch.Client({
host: 'http://localhost:9200',
apiVersion: '7.0'
})
});
const Asset = composeWithElastic({
graphqlTypeName: 'AssetType',
elasticIndex: 'assets',
elasticType: '_doc',
elasticMapping: asset_mapping,
elasticClient: new elasticsearch.Client({
host: 'http://localhost:9200',
apiVersion: '7.0'
})
});
const schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'Query',
fields: {
roomSearch: Room.getResolver('search').getFieldConfig(),
roomSearchConnection: Room.getResolver('searchConnection').getFieldConfig(),
elastic70: elasticApiFieldConfig({
host: 'http://localhost:9200',
apiVersion: '7.0'
})
}
})
});
module.default = { schema };
server.js is like this:
const graphqlHTTP = require('express-graphql');
const { schema } = require('./schema/schema_es');
// const schema = require('./schema/schema');
var cors = require('cors');
const server = require('express')();
//allow cross-origin
server.use(
'/',
cors(),
graphqlHTTP({
schema: schema,
graphiql: true,
formatError: (error) => ({
message: error.message,
stack: error.stack.split('\n')
})
})
);
const PORT = process.env.PORT || 6060;
server.listen(PORT, () => console.log(`Server started on port ${PORT}`));
I restarted the servers, and here is the error I got
invariant.esm.js:31 [GraphQL error]: Message: GraphQL middleware options must contain a schema., Location: undefined, Path: undefined
I have no idea how to how to transfrom graphql schema, rootquery..etc into ES query using this graphql-compose-elasticsearch, any help is appreciated!