I am new to GraphQL not sure what I am doing wrong. Any help will be appreciated. I have included my GraphQL schema, data and query here. I am getting exception in query "Value to set in Organization.relationships in not normalizable: Value to set in Relationships.customers in not normalizable: should be an object or null or undefined".
My Schema:
type Query{
searchForOrganization: [Organization!]!
}
type Data{
id: ID
type: String
}
type Group{
data:[Data]
}
type User{
data:[Data]
}
type Customer{
data:[Data]
}
type Relationships{
groups: Group
users: User
customers: Customer
}
type Attributes{
name: String!
description: String
authenticationUrl: String
}
type Organization{
id: ID!
type: String!
attributes: Attributes!
relationships: Relationships!
}
Mock Data:
const mocks = {
Query: () => ({
searchForOrganization: () => [...new Array(9)],
}),
Organization: () => (
{
type: () => "organization",
id: () =>"Test002",
attributes: ()=>{
return{
authenticationUrl: "XXXX",
description: "Test Company",
"name": "Test Company"
};
},
relationships: () => {
return{
customers: () => {
return{
data: [{type: "customer", id: "Test002_Root_customers"},
{type: "customer", id: "Test003_Root_customers"}
]
};},
groups: () => {
return{
data: [{type: "group", id: "Test003_Root_groups"},
{type: "group", id: "Test002_Root_groups"}]
};},
users: () => {
return{
data: [{type: "user", id: "Test003_Root_users"},
{type: "user", id: "Test002_Root_users"}]
};}
};
}
}
),
};
My Query:
query SearchForOrganization {
searchForOrganization {
id
type
attributes {
name
description
authenticationUrl
}
relationships {
groups {
data {
id
type
}
}
users {
data {
id
type
}
}
customers {
data {
id
type
}
}
}
}
}
My Result:
{
"errors": [
{
"message": "Value to set in Organization.relationships in not normalizable: Value to set in Relationships.customers in not normalizable: should be an object or null or undefined. Received () => {\r\n return{\r\n data: [{type: \"customer\", id: \"Test002_Root_customers\"},\r\n {type: \"customer\", id: \"Test003_Root_customers\"}\r\n ]\r\n };}",
"locations": [
{
"line": 10,
"column": 5
}
],
"path": [
"searchForOrganization",
0,
"relationships"
],
"extensions": {
"code": "INTERNAL_SERVER_ERROR",
"exception": {
"stacktrace": [
"Error: Value to set in Organization.relationships in not normalizable: Value to set in Relationships.customers in not normalizable: should be an object or null or undefined. Received () => {\r",
" return{\r",
" data: [{type: \"customer\", id: \"Test002_Root_customers\"},\r",
" {type: \"customer\", id: \"Test003_Root_customers\"}\r",
" ]\r",
" };}",
Change in schema solved the issue
type TypeAndID{
id: ID
type: String
}
type DataGroup{
data:[TypeAndID]
}
type OrgRelationships{
groups: DataGroup
users: DataGroup
customers: DataGroup
}
type Attributes{
name: String!
description: String
authenticationUrl: String
}
type Organization{
id: ID!
type: String!
attributes: Attributes!
relationships: OrgRelationships!
}
type Data{
data: [Organization!]!
}
type Query{
getOrganizations: Data
}
Related
GraphQL Query
Morning all,
I have a nice setup GraphQL -> Sequalize -> Existing DB (generated by a Laravel application). I've built this schema out:
type App {
id: ID!
userId: ID
user: User
pages: [Page]
experiments: [Experiment]
uri: String!
createdAt: String!
updatedAt: String!
}
type Page {
id: ID!
userId: ID
user: User
appId: ID!
app: App!
uri: String!
createdAt: String!
updatedAt: String!
}
type Experiment {
id: ID!
title: String!
appId: ID!
app: App!
createdAt: String!
updatedAt: String!
}
Based on the existing data. Querying an apps experiments works just great:
query {
app(id: 6) {
id
title
experiments {
id
}
}
}
{
"data": {
"app": {
"id": "6",
"title": "C-Map Embark: Boating",
"experiments": [
{
"id": "1"
}
]
}
}
}
But querying pages I get this:
query {
app(id: 6) {
id
title
pages {
id
}
}
}
{
"errors": [
{
"message": "parent.getPages is not a function",
"locations": [
{
"line": 5,
"column": 5
}
],
"path": [
"app",
"pages"
],
"extensions": {
"code": "INTERNAL_SERVER_ERROR",
"exception": {
"stacktrace": [
"TypeError: parent.getPages is not a function",
...
The db columns are the same, as are the resolvers:
/* jshint indent: 2 */
module.exports = function(sequelize, DataTypes) {
const Page = sequelize.define(
"page",
{
id: {
type: DataTypes.INTEGER(10).UNSIGNED,
allowNull: false,
primaryKey: true,
autoIncrement: true
},
...
createdAt: {
type: DataTypes.DATE,
allowNull: true
},
updatedAt: {
type: DataTypes.DATE,
allowNull: true
}
},
{
tableName: "pages",
underscored: true
}
);
Page.associate = models => {
Page.belongsTo(models.app);
};
return Page;
};
/* jshint indent: 2 */
module.exports = function(sequelize, DataTypes) {
const Experiment = sequelize.define(
"experiment",
{
id: {
type: DataTypes.INTEGER(10).UNSIGNED,
allowNull: false,
primaryKey: true,
autoIncrement: true
},
...
createdAt: {
type: DataTypes.DATE,
allowNull: true
},
updatedAt: {
type: DataTypes.DATE,
allowNull: true
}
},
{
tableName: "experiments",
underscored: true
}
);
Experiment.associate = models => {
Experiment.belongsTo(models.app);
};
return Experiment;
};
Have you come across this before?
my whole schema
const Films = new GraphQLObjectType({
name: 'films',
interfaces: () => [MovieStream],
fields: () => ({
movie_id: {
type: GraphQLString,
},
id:{
type: GraphQLID
},
name: {
type: GraphQLString,
},
})
})
Films._typeConfig = {
sqlTable: "films",
uniqueKey: 'id',
}
const MovieStream = new GraphQLInterfaceType({
name: 'MovieStream',
fields: () => ({
id: {
type: GraphQLID,
},
movie_id: {
type: GraphQLString,
},
})
})
MovieStream._typeConfig = {
sqlTable: "movie_streams",
uniqueKey: 'id'
}
const QueryRoot = new GraphQLObjectType({
name: 'Query',
fields: () => ({
getContentList:{
type: new GraphQLList(Films),
args: {
id: {
type: GraphQLInt
},
permalink: {
type: GraphQLString
},
language: {
type: GraphQLString
},
content_types_id: {
type: GraphQLString
},
oauth_token:{
type: GraphQLString
}
},
resolve: (parent, args, context, resolveInfo) => {
return joinMonster.default(resolveInfo,{}, sql => {
return FilmDb.query(sql).then(function(result) {
return result[0];
});
} ,{dialect: 'mysql'});
},
}
})
})
module.exports = new GraphQLSchema({
query: QueryRoot
})
I have again modified my code still got the error
{
"errors": [
{
"message": "Unknown column 'getContent.movie_id' in 'field list'",
"locations": [
{
"line": 2,
"column": 3
}
],
"path": [
"getContentList"
]
}
],
"data": {
"getContentList": null
}
}
My previous post Is it possible to fetch data from multiple tables using GraphQLList
Please check and tell me where i am wrong??? I have already add the field still it does not access the field of that object type
My sampleJSON -
{
"entries": [
{
"fields":{
"title":"My test title"
}
},
{
"fields":{
"description":"My test description"
}
}
]
}
Schema.js -
const rootQuery = new GraphQLObjectType({
name: 'testQuery',
fields: {
Articles: {
type: articleItem,
resolve(parentValue) {
return axios.get(`/getArticles`).then(resp => resp.data);
}
}
}
});
const articleItem = new GraphQLObjectType({
name: 'articleItem',
fields: () => ({
entries: {type: new GraphQLList(entry)}
})
});
const entry = new GraphQLObjectType({
name: 'entry',
fields: () => ({
fields: {type: fields}
})
});
const fields = new GraphQLObjectType({
name: 'fields',
fields: () => ({
title: {type: GraphQLString},
description: {type: GraphQLString}
})
});
GraphQL query i am using to query the data in the above JSON -
query articles{
Articles {
entries{
fields{
title,
description
}
}
}
}
I am wondering why the query returns "title" even though it is null in the second object and likewise with description in the first object. Is there a way to just return " title " or " description " only if it not null?
Current result of the query -
{
"data" : {
"entries" [
{
"fields": {
"title": "My test title",
"description": null
}
},
{
"fields": {
"title": null,
"description" : "My test description"
}
}
]
}
}
Required result -
{
"data" : {
"entries" [
{
"fields": {
"title": "My test title"
}
},
{
"fields": {
"description" : "My test description"
}
}
]
}
}
Appreciate any help with this !, thanks.
Way too late to answer, but if you stumble upon this, you can make non-nullable (!) by using GraphQLNonNull().
Here is the example for non-nullable integer
random: {
type: new GraphQLNonNull(GraphQLInt)
}
Is there a way to produce static values in a graphql query?
For example, let's say that I have a user object with a name and email field. For some reason, I always want the status of a user to be "ACCEPTED". How can I write a query that accomplishes this?
What I want to do:
query {
user(id: 1) {
email
name
status: "ACCEPTED"
}
}
The result I want:
{
"data": {
"user": {
"email": "me#myapp.com",
"name": "me",
"status": "ACCEPTED"
}
}
}
You can make your resolve function return a static value, e.g. like this in JavaScript:
const HomeWorldType = new GraphQLObjectType({
name: 'HomeWorld',
fields: () => {
return {
id: {
type: GraphQLInt,
resolve: () => 7,
},
name: { type: GraphQLString },
climate: { type: GraphQLString },
population: { type: GraphQLString },
}
}
})
I have this json object
{
id: string,
name: string,
category: {
id: string
name: string,
}
}
I want to have column that bind to productCategory.name. However that field is nullable. When productCategory is null/undefined, kendo will throw error. How can i tell kendo that if field is undefined, just show empty string?
EDIT
Below is my sample data
[{
"id":1,
"name":"Spaghetti",
"category":{
"id":"1",
"name":"Food"
}},
{
"id":2,
"name":"Coca-cola",
"category":null
}}]
Below is my kendo datasource
var kendoDataSource = new kendo.data.DataSource({
schema: {
data: "data",
total: "total",
model: {
id: "id",
fields: {
id: { type: "string" },
name: { type: "string" },
"category.name": { type: "string" }
}
}
}
});
Data above will throw "undefined" error, because second product does not have category.
Try using empty object instead of null
created a fiddle,check this:
http://jsfiddle.net/Sowjanya51/h930jcru/
Just provide a default value in your model like this:
var kendoDataSource = new kendo.data.DataSource({
schema: {
data: "data",
total: "total",
model: {
id: "id",
fields: {
id: { type: "string" },
name: { type: "string" },
"category.name": { type: "string" },
category: { defaultValue: {
id: "",
name: "",
}
}
}
}
}
});
This will initialize productCategory if it is null or undefined.