How to implement interface using GraphQL and node - graphql

I want to achieve the fields of one object type within another object type
Here is my schema file.
const Films = new GraphQLInterfaceType({
name: 'films',
fields: () => ({
id:{
type: GraphQLID
},
name: {
type: GraphQLString,
},
})
})
const MovieStream = new GraphQLObjectType({
name: 'MovieStream',
interfaces: () => [Films],
fields: () => ({
id: {
type: GraphQLID,
},
movie_id: {
type: GraphQLString,
},
})
})
Here I am trying to use the interface. But It shows error:
{
"errors": [
{
"message": "Query root type must be Object type, it cannot be { __validationErrors: undefined, __allowedLegacyNames: [], _queryType: undefined, _mutationType: undefined, _subscriptionType: undefined, _directives: [#include, #skip, #deprecated], astNode: undefined, extensionASTNodes: undefined, _typeMap: { __Schema: __Schema, __Type: __Type, __TypeKind: __TypeKind, String: String, Boolean: Boolean, __Field: __Field, __InputValue: __InputValue, __EnumValue: __EnumValue, __Directive: __Directive, __DirectiveLocation: __DirectiveLocation, films: films, ID: ID, Date: Date, JSON: JSON, MovieStream: MovieStream }, _possibleTypeMap: {}, _implementations: { films: [] } }."
},
{
"message": "Expected GraphQL named type but got: { __validationErrors: undefined, __allowedLegacyNames: [], _queryType: undefined, _mutationType: undefined, _subscriptionType: undefined, _directives: [#include, #skip, #deprecated], astNode: undefined, extensionASTNodes: undefined, _typeMap: { __Schema: __Schema, __Type: __Type, __TypeKind: __TypeKind, String: String, Boolean: Boolean, __Field: __Field, __InputValue: __InputValue, __EnumValue: __EnumValue, __Directive: __Directive, __DirectiveLocation: __DirectiveLocation, films: films, ID: ID, Date: Date, JSON: JSON, MovieStream: MovieStream }, _possibleTypeMap: {}, _implementations: { films: [] } }."
}
]
}
Here is Query type:
const QueryRoot = new GraphQLObjectType({
name: 'Query',
fields: () => ({
getContentList:{
type: new GraphQLList(contentCategory),
args: {
id: {
type: GraphQLInt
},
permalink: {
type: GraphQLString
},
language: {
type: GraphQLString
},
content_types_id: {
type: GraphQLString
},
oauth_token:{
type: GraphQLString
}
},
resolve: (parent, args, context, resolveInfo) => {
var category_flag = 0;
var menuItemInfo = '';
user_id = args.user_id ? args.user_id : 0;
// console.log("context"+context['oauth_token']);
return AuthDb.models.oauth_registration.findAll({attributes: ['oauth_token', 'studio_id'],where:{
// oauth_token:context['oauth_token'],
$or: [
{
oauth_token:
{
$eq: context['oauth_token']
}
},
{
oauth_token:
{
$eq: args.oauth_token
}
},
]
},limit:1}).then(oauth_registration => {
var oauthRegistration = oauth_registration[0]
// for(var i = 0;i<=oauth_registration.ength;i++){
if(oauth_registration && oauthRegistration && oauthRegistration.oauth_token == context['oauth_token'] || oauthRegistration.oauth_token == args.oauth_token){
studio_id = oauthRegistration.studio_id;
return joinMonster.default(resolveInfo,{}, sql => {
return contentCategoryDb.query(sql).then(function(result) {
return result[0];
});
} ,{dialect: 'mysql'});
}else{
throw new Error('Invalid OAuth Token');
}
})
},
where: (filmTable, args, context) => {
return getLanguage_id(args.language).then(language_id=>{
return ` ${filmTable}.permalink = "${args.permalink}" and ${filmTable}.studio_id = "${studio_id}" and (${filmTable}.language_id = "${language_id}" OR ${filmTable}.parent_id = 0 AND ${filmTable}.id NOT IN (SELECT ${filmTable}.parent_id FROM content_category WHERE ${filmTable}.permalink = "${args.permalink}" and ${filmTable}.language_id = "${language_id}" and ${filmTable}.studio_id = "${studio_id}"))`
})
},
}
})
})
module.exports = new GraphQLSchema({
query: QueryRoot
})
Please help me out. have i done something wrong in the use of interface?
I have found the answer through this post
Is it possible to fetch data from multiple tables using GraphQLList
Anyone please tell me the exact way to use the interface in my code.

Although the error you have printed does not really relate to interfaces implementations, in order for you to use interfaces, you have to implement the methods/types the interface references. So in your situation your object MovieStream is missing the type name that you refer in the object Films.
Your code should look something like:
const Films = new GraphQLInterfaceType({
name: 'films',
fields: () => ({
id:{
type: GraphQLID
},
name: {
type: GraphQLString,
},
})
})
const MovieStream = new GraphQLObjectType({
name: 'MovieStream',
interfaces: () => [Films],
fields: () => ({
id: {
type: GraphQLID,
},
name: {
type: GraphQLString // You're missing this!
},
movie_id: {
type: GraphQLString,
},
})
})
Now back to the error you have printed "message": "Query root type must be Object type, it cannot be...
This seems to be related to your QueryRoot object, it seems that GraphQLSchema is not recognizing the root object. If this issue is still there once you fix the interface, have a look at this answer here

Related

GraphQL error: Expected GraphQL named type but got: {}

I have 2 custom graphql types, entityType and containerType, where 1 container can have multiple entities.
So, I am binding all the entities with their respective containers via this code:
const graphql = require('graphql')
const _ = require('lodash')
const UserType = require('./userSchema')
const ContainerType = require('./containerSchema')
const Container = require('../models/container')
const Entity = require('../models/entity')
const {
GraphQLObjectType,
GraphQLString,
GraphQLBoolean,
GraphQLSchema,
GraphQLInt,
GraphQLID,
GraphQLList,
GraphQLNonNull,
GraphQLUnionType
} = graphql
const EntityType = new GraphQLObjectType({
name: "Entity",
fields: () => ({
name: { type: new GraphQLNonNull(GraphQLString) },
container: {
type: ContainerType,
resolve: function(parent, args) {
return Container.findById(parent.containerId)
}
},
type: { type: new GraphQLNonNull(GraphQLString) },
detail: { type: new GraphQLNonNull(GraphQLString) },
start: { type: GraphQLString },
end: { type: GraphQLString }
})
})
module.exports = { EntityType }
I am quite sure that containerType is working, because I am using it is being used on other places and is working well. Here is the code for Container type:
const graphql = require('graphql')
const _ = require('lodash')
const UserType = require('./userSchema')
const { EntityType } = require('./entitySchema')
const User = require('../models/user')
const Container = require('../models/container')
const Entity = require('../models/entity')
const {
GraphQLObjectType,
GraphQLString,
GraphQLBoolean,
GraphQLSchema,
GraphQLInt,
GraphQLID,
GraphQLList,
GraphQLNonNull
} = graphql
const ContainerType = new GraphQLObjectType ({
name: 'Container',
fields: () => ({
id: { type: GraphQLID },
name: { type: GraphQLString },
user: {
type: UserType,
resolve: function (parent, args) {
return User.findById(parent.userId)
}
},
parent: {
type: ContainerType,
resolve: function (parent, args) {
return Container.findById(parent.parentContainer)
}
},
detail: { type: GraphQLString },
start: { type: GraphQLString },
end: { type: GraphQLString },
createdAt: { type: GraphQLString },
category: { type: GraphQLString },
status: { type: GraphQLString },
entities: {
type: GraphQLList(EntityType),
resolve: async function(parent, args) {
return await Entity.find({ containerId: parent.id })
}
}
})
})
module.exports = ContainerType
no error is shown on the terminal, but the Graphiql console shows following error when loading up:
{
"errors": [
{
"message": "The type of Entity.container must be Output Type but got: {}."
},
{
"message": "Expected GraphQL named type but got: {}."
}
]
}
I figured out that the problem was with something called "module cycles". this post was very helpful to resolve it.
Finally, I ended up adding both EntitySchema and ContainerSchema in a single file, here is the code:
const graphql = require('graphql')
const _ = require('lodash')
const UserType = require('./userSchema')
const User = require('../models/user')
const Container = require('../models/container')
const Entity = require('../models/entity')
const {
GraphQLObjectType,
GraphQLString,
GraphQLBoolean,
GraphQLInt,
GraphQLID,
GraphQLList,
GraphQLNonNull
} = graphql
const EntityType = new GraphQLObjectType ({
name: "Entity",
fields: () => ({
name: { type: new GraphQLNonNull(GraphQLString) },
container: {
type: ContainerType,
resolve: (parent, args) => {
return Container.findById(parent.containerId)
}
},
type: { type: new GraphQLNonNull(GraphQLString) },
detail: { type: new GraphQLNonNull(GraphQLString) },
start: { type: GraphQLString },
end: { type: GraphQLString }
})
})
const ContainerType = new GraphQLObjectType ({
name: 'Container',
fields: () => ({
id: { type: GraphQLID },
name: { type: GraphQLString },
user: {
type: UserType,
resolve: function (parent, args) {
return User.findById(parent.userId)
}
},
parent: {
type: ContainerType,
resolve: function (parent, args) {
return Container.findById(parent.parentContainer)
}
},
detail: { type: GraphQLString },
start: { type: GraphQLString },
end: { type: GraphQLString },
createdAt: { type: GraphQLString },
category: { type: GraphQLString },
status: { type: GraphQLString },
entities: {
type: GraphQLList(EntityType),
resolve: async function(parent, args) {
return await Entity.find({ containerId: parent.id })
}
}
})
})
module.exports = {
EntityType,
ContainerType
}

GraphQL Error: "Expected Iterable, but did not find one for field Query" when fetching data from CryptoCompare API

I'm using GraphQL/express/express-graphql/axios to retrieve specific coin data from the CryptoCompare API.
I have two endpoints:
1) https://min-api.cryptocompare.com/data/pricemultifull?fsyms=BTC,ETH,LTC,BCH,NEO,ETC,XMR&tsyms=USD
From endpoint 1, I want to retrieve the following in USD for 8 coins:
- FROMSYMBOL, CHANGEPCT24HOUR, PRICE, MKTCAP, TOTALVOLUME24HTO
2) https://min-api.cryptocompare.com/data/coin/generalinfo?fsyms=BTC&tsym=USD
From endpoint 2, I want to retrieve the following just for Bitcoin/BTC:
- Id, FullName, ImageUrl
I have setup my backend server with two files, as well as testing queries using graphiql.
File 1 - server.js
const express = require("express")
const graphqlHTTP = require("express-graphql")
const cors = require("cors")
const schema = require("./schema")
const app = express()
app.use(cors())
app.use(
"/graphql",
graphqlHTTP({
schema,
graphiql: true
})
)
const PORT = process.env.PORT || 4000
app.listen(PORT, console.log(`✅ Listening to port ${PORT}`))
File 2 - schema.js
const {
GraphQLObjectType,
GraphQLList,
GraphQLID,
GraphQLInt,
GraphQLString,
GraphQLSchema
} = require("graphql")
const axios = require("axios")
const CoinDataType = new GraphQLObjectType({
name: "CoinData",
fields: () => ({
FROMSYMBOL: { type: GraphQLString },
CHANGEPCT24HOUR: { type: GraphQLInt },
PRICE: { type: GraphQLInt },
MKTCAP: { type: GraphQLInt },
TOTALVOLUME24HTO: { type: GraphQLInt }
})
})
const CoinInfoType = new GraphQLObjectType({
name: "CoinInfo",
fields: () => ({
Id: { type: GraphQLID },
FullName: { type: GraphQLString },
ImageUrl: { type: GraphQLString }
})
})
const Query = new GraphQLObjectType({
name: "Query",
fields: {
CoinData: {
type: new GraphQLList(CoinDataType),
resolve(parent, args) {
return axios
.get(
"https://min-api.cryptocompare.com/data/pricemultifull?fsyms=BTC,ETH,LTC,BCH,NEO,ETC,XMR&tsyms=USD"
)
.then(res => res.data)
}
},
CoinInfo: {
type: new GraphQLList(CoinInfoType),
resolve(parent, args) {
return axios
.get(
"https://min-api.cryptocompare.com/data/coin/generalinfo?fsyms=BTC&tsym=USD"
)
.then(res => res.data)
}
}
}
})
module.exports = new GraphQLSchema({ query: Query })
When I use graphiql to test my queries with this:
{
CoinData {
FROMSYMBOL
}
CoinInfo {
Id
}
}
...I get this error:
{
"errors": [
{
"message": "Expected Iterable, but did not find one for field Query.CoinInfo.",
"locations": [
{
"line": 5,
"column": 3
}
],
"path": [
"CoinInfo"
]
},
{
"message": "Expected Iterable, but did not find one for field Query.CoinData.",
"locations": [
{
"line": 2,
"column": 3
}
],
"path": [
"CoinData"
]
}
],
"data": {
"CoinData": null,
"CoinInfo": null
}
}
How do I get around this error? Thanks.

Issue in querying graphQL relay calls (.then is not a function)

I have a graphql server running which I am using for query one of the object that gets instantiated when server starts (kind of in-memory db). Here company object is created every time schema is loaded or say server is started which I am using in QueryType object to resolve.
Here is the graphQL Schema
const {
GraphQLSchema,
GraphQLObjectType,
GraphQLString,
GraphQLInt,
GraphQLList
} = require('graphql');
const {
connectionDefinitions,
connectionArgs,
connectionFromArray,
connectionFromPromisedArray
} = require('graphql-relay');
//**************************** In-Memory Data ********************************//
var company = {
id:'123456',
customFieldDefinitions:[
{
name: 'cfm1',
id: '123'
},
{
name: 'cfm2',
id: '1234'
}
]
};
//**************************** In-Memory Code Ends *********************************//
const CustomFieldDefinitionType = new GraphQLObjectType({
name: 'Common_CustomFieldDefinitionsConnection',
fields: {
id: {
type: GraphQLString,
resolve: (cfd) => cfd.id
},
name: {
type: GraphQLString,
resolve: (cfd) => cfd.name
}
}
});
const { connectionType: CustomFieldDefinitionConnection } =
connectionDefinitions({
name: 'Common_CustomFieldDefinition',
nodeType: CustomFieldDefinitionType
});
const CompanyType = new GraphQLObjectType({
name: 'Company',
fields: {
id: {
type: GraphQLString,
resolve: (obj) => obj.id
},
customFieldDefinitions: {
type: CustomFieldDefinitionConnection,
description: 'A list of Custom Fields',
args: connectionArgs,
resolve: (obj, args) => connectionFromPromisedArray(obj.customFieldDefinitions, args)
}
}
});
const QueryType = new GraphQLObjectType({
name: 'Query',
fields: {
company: {
args: {
id: { type: GraphQLString },
},
type: CompanyType,
resolve: (_, args) => company
}
}
});
const mySchema = new GraphQLSchema({
query: QueryType
});
module.exports = mySchema;
Now when I try to query as below on graphiQL editor
query{
company{
customFieldDefinitions {
edges {
node {
id
}
}
}
}
}
I get error as below.
{
"data": {
"company": {
"customFieldDefinitions": null
}
},
"errors": [
{
"message": "dataPromise.then is not a function",
"locations": [
{
"line": 3,
"column": 5
}
],
"path": [
"company",
"customFieldDefinitions"
]
}
]
}
How can I identify the problem?
It looks like the error is with this line:
resolve: (obj, args) => connectionFromPromisedArray(obj.customFieldDefinitions, args)
The problem is that the connectionFromPromisedArray function imported from graphql-relay expects a promise that returns an array and obj.customFieldDefinitions is a normal array. Just to trace it back, obj is the parent element of that resolver which in this case is what you return from the company resolver on the Query type.
To fix it, either change connectionFromPromisedArray to connectionFromArray or change your company object to this:
var company = {
id:'123456',
customFieldDefinitions: Promise.resolve([
{
name: 'cfm1',
id: '123'
},
{
name: 'cfm2',
id: '1234'
}
])
};

Filters in GraphQL

const { connectionType: PersonConnection } = connectionDefinitions({
name: 'Person',
nodeType: PersonType,
here i am using connectionFields for count
connectionFields: {
count: {
type: GraphQLInt,
resolve: (args) => {
const filter = args.args || {};
return Person.count(filter).exec();
},
},
},
});
i am quite confused about using args with custom filters and obtain data from database, using filter
if i don't provide any id count should provide all data count, if i provide any id it may also look for references data and search in another models so how to perform the count and efficient filteration of data.
Thanks in Advance
person: {
type: PersonConnection,
args: _.assign({
_id: { type: GraphQLID },
// assign mine custom filters
name: { type: GraphQLString },
location: { type: GraphQLString },
education: { type: GraphQLString },
}, connectionArgs),
resolve: (obj, args, auth, fieldASTs) => {
const filter = args;
return connectionFromPromisedArray(getPersons(filter, fieldASTs), args).then((data) => {
// using to connection Fields
data.args = filter;
return data;
}).catch(err => new Error(err));
},
},

GraphQL: How do you pass args to to sub objects

I am using GraphQL to query an object that will be composed from about 15 different REST calls. This is my root query in which I pass in in the ID from the query. This works fine for the main student object that resolves correctly. However, I need to figure out how to pass the ID down to the address resolver. I tried adding args to the address object but I get an error that indicates that the args are not passed down from the Student object. So my question is: How do I pass arguments from the client query to sub objects in a GraphQL server?
let rootQuery = new GraphQLObjectType({
name: 'Query',
description: `The root query`,
fields: () => ({
Student : {
type: Student ,
args: {
id: {
name: 'id',
type: new GraphQLNonNull(GraphQLString)
}
},
resolve: (obj, args, ast) => {
return Resolver(args.id).Student();
}
}
})
});
export default rootQuery;
This is my primary student object that I link the other objects. In this case I have attached the ADDRESS object.
import {
GraphQLInt,
GraphQLObjectType,
GraphQLString,
GraphQLNonNull,
GraphQLList
} from 'graphql';
import Resolver from '../../resolver.js'
import iAddressType from './address.js'
let Student = new GraphQLObjectType({
name: 'STUDENT',
fields: () => ({
SCHOOLCODE: { type: GraphQLString },
LASTNAME: { type: GraphQLString },
ACCOUNTID: { type: GraphQLInt },
ALIENIDNUMBER: { type: GraphQLInt },
MIDDLEINITIAL: { type: GraphQLString },
DATELASTCHANGED: { type: GraphQLString },
ENROLLDATE: { type: GraphQLString },
FIRSTNAME: { type: GraphQLString },
DRIVERSLICENSESTATE: { type: GraphQLString },
ENROLLMENTSOURCE: { type: GraphQLString },
ADDRESSES: {
type: new GraphQLList(Address),
resolve(obj, args, ast){
return Resolver(args.id).Address();
}}
})
});
Here is my address object that is resolved by a second REST call:
let Address = new GraphQLObjectType({
name: 'ADDRESS',
fields: () => ({
ACTIVE: { type: GraphQLString },
ADDRESS1: { type: GraphQLString },
ADDRESS2: { type: GraphQLString },
ADDRESS3: { type: GraphQLString },
CAMPAIGN: { type: GraphQLString },
CITY: { type: GraphQLString },
STATE: { type: GraphQLString },
STATUS: { type: GraphQLString },
TIMECREATED: { type: GraphQLString },
TYPE: { type: GraphQLString },
ZIP: { type: GraphQLString },
})
});
export default Address;
These are my resolver
var Resolver = (id) => {
var options = {
hostname: "myhostname",
port: 4000
};
var GetPromise = (options, id, path) => {
return new Promise((resolve, reject) => {
http.get(options, (response) => {
var completeResponse = '';
response.on('data', (chunk) => {
completeResponse += chunk;
});
response.on('end', () => {
parser.parseString(completeResponse, (err, result) => {
let pathElements = path.split('.');
resolve(result[pathElements[0]][pathElements[1]]);
});
});
}).on('error', (e) => { });
});
};
let Student= () => {
options.path = '/Student/' + id;
return GetPromise(options, id, 'GetStudentResult.StudentINFO');
}
let Address= () => {
options.path = '/Address/' + id + '/All';
return GetPromise(options, id, 'getAddressResult.ADDRESS');
};
return {
Student,
Address
};
}
export default Resolver;
ADDRESSES: {
type: new GraphQLList(Address),
resolve(obj, args, ast){
return Resolver(args.id).Address();
}
}
args passed to ADDRESSES are arguments passed to ADDRESSES field at query time. In the resolve method, obj should be the student object and if you have an id property on it, all you need to do is: return Resolver(obj.id).Address();.

Resources