Query with different types in GraphQL
So I have a graphQL userType
and EventType
and on my userType
I have to make a nested query using EventType
but I also need to return user information but since EventType
doesn't have fields same as user info so to pass my code on testing I had to manually copy-paste UserType
to EventType
but I dont want this to solution. is there a way to like merge types? I'm using mongoDB as my database so when trying to do a POPULATE
the user info comes back but Graphql making it hard. sorry im still new to graphql.
so this is what I did on EventType
to pass the test. under fixme i copy the UserType
module.exports = {
EventType: new GraphQLObjectType({
name: 'Event',
fields: () => ({
id: {type: GraphQLID},
organizerId: {type: new GraphQLList(GraphQLID)},
title: {type: GraphQLString},
image: {type: GraphQLString},
description: {type: GraphQLString},
location: {type: GraphQLString},
items: {type: GraphQLInt},
date: {type: GraphQLString},
attendeesId: {type: new GraphQLList(GraphQLID)},
supplies: {type: new GraphQLList(GraphQLString)},
test: {type: GraphQLString},
// FIXME: USER TYPES!
firstName: {type: GraphQLString},
lastName: {type: GraphQLString},
email: {type: GraphQLString},
age: {type: GraphQLInt},
token: {type: GraphQLString},
image: {type: GraphQLString},
phone: {type: GraphQLInt},
address: {type: GraphQLString},
// ! TYPE RELATION
userRelatedToEvent: {
type: UserType,
resolve: async (parent, args) => {
const id = {
id: parent.organizerId
};
return await getCurrentUser(id);
}
}
})
})
};
this is my code on UserType
that is doing a Type relation on EventType
see resolve
prop
module.exports = {
UserType: new GraphQLObjectType({
name: 'User',
fields: () => ({
id: {type: GraphQLID},
firstName: {type: GraphQLString},
lastName: {type: GraphQLString},
email: {type: GraphQLString},
age: {type: GraphQLInt},
token: {type: GraphQLString},
image: {type: GraphQLString},
phone: {type: GraphQLInt},
address: {type: GraphQLString},
eventsId: {type: new GraphQLList(GraphQLID)}, // <-- this will be the query for userRelatedToUser
statusCode: {type: GraphQLInt},
isSuccess: {type: GraphQLBoolean},
msg: {type: GraphQLString},
test: {type: GraphQLString}, // testing query
// errors: { type: GraphQ LString },
eventRelatedToUser: {
type: require('./eventTypeDef').EventType,
resolve: async (parent, args) => {
const event = {
event: parent.eventsId
};
const test = await getEventWithEventId(event); // <-- queries event
const id = {
id: test.attendeesId
};
const users = await getCurrentUser(id); // <-- user info
console.log(users);
return {...users, ...test};
}
}
})
})
};
graphql
add a comment |
So I have a graphQL userType
and EventType
and on my userType
I have to make a nested query using EventType
but I also need to return user information but since EventType
doesn't have fields same as user info so to pass my code on testing I had to manually copy-paste UserType
to EventType
but I dont want this to solution. is there a way to like merge types? I'm using mongoDB as my database so when trying to do a POPULATE
the user info comes back but Graphql making it hard. sorry im still new to graphql.
so this is what I did on EventType
to pass the test. under fixme i copy the UserType
module.exports = {
EventType: new GraphQLObjectType({
name: 'Event',
fields: () => ({
id: {type: GraphQLID},
organizerId: {type: new GraphQLList(GraphQLID)},
title: {type: GraphQLString},
image: {type: GraphQLString},
description: {type: GraphQLString},
location: {type: GraphQLString},
items: {type: GraphQLInt},
date: {type: GraphQLString},
attendeesId: {type: new GraphQLList(GraphQLID)},
supplies: {type: new GraphQLList(GraphQLString)},
test: {type: GraphQLString},
// FIXME: USER TYPES!
firstName: {type: GraphQLString},
lastName: {type: GraphQLString},
email: {type: GraphQLString},
age: {type: GraphQLInt},
token: {type: GraphQLString},
image: {type: GraphQLString},
phone: {type: GraphQLInt},
address: {type: GraphQLString},
// ! TYPE RELATION
userRelatedToEvent: {
type: UserType,
resolve: async (parent, args) => {
const id = {
id: parent.organizerId
};
return await getCurrentUser(id);
}
}
})
})
};
this is my code on UserType
that is doing a Type relation on EventType
see resolve
prop
module.exports = {
UserType: new GraphQLObjectType({
name: 'User',
fields: () => ({
id: {type: GraphQLID},
firstName: {type: GraphQLString},
lastName: {type: GraphQLString},
email: {type: GraphQLString},
age: {type: GraphQLInt},
token: {type: GraphQLString},
image: {type: GraphQLString},
phone: {type: GraphQLInt},
address: {type: GraphQLString},
eventsId: {type: new GraphQLList(GraphQLID)}, // <-- this will be the query for userRelatedToUser
statusCode: {type: GraphQLInt},
isSuccess: {type: GraphQLBoolean},
msg: {type: GraphQLString},
test: {type: GraphQLString}, // testing query
// errors: { type: GraphQ LString },
eventRelatedToUser: {
type: require('./eventTypeDef').EventType,
resolve: async (parent, args) => {
const event = {
event: parent.eventsId
};
const test = await getEventWithEventId(event); // <-- queries event
const id = {
id: test.attendeesId
};
const users = await getCurrentUser(id); // <-- user info
console.log(users);
return {...users, ...test};
}
}
})
})
};
graphql
Does an event itself have a first name, last name, etc., or are those fields copies of the fields on the associated user?
– David Maze
Jan 3 at 18:36
those are copies. thats what im trying to figure out. to associate both fields. is there such way? @DavidMaze
– artoo
Jan 3 at 19:35
The ordinary GraphQL way would be to not have those fields directly; a query likesomeEvent { user { firstName } }
would retrieve it.
– David Maze
Jan 3 at 22:49
add a comment |
So I have a graphQL userType
and EventType
and on my userType
I have to make a nested query using EventType
but I also need to return user information but since EventType
doesn't have fields same as user info so to pass my code on testing I had to manually copy-paste UserType
to EventType
but I dont want this to solution. is there a way to like merge types? I'm using mongoDB as my database so when trying to do a POPULATE
the user info comes back but Graphql making it hard. sorry im still new to graphql.
so this is what I did on EventType
to pass the test. under fixme i copy the UserType
module.exports = {
EventType: new GraphQLObjectType({
name: 'Event',
fields: () => ({
id: {type: GraphQLID},
organizerId: {type: new GraphQLList(GraphQLID)},
title: {type: GraphQLString},
image: {type: GraphQLString},
description: {type: GraphQLString},
location: {type: GraphQLString},
items: {type: GraphQLInt},
date: {type: GraphQLString},
attendeesId: {type: new GraphQLList(GraphQLID)},
supplies: {type: new GraphQLList(GraphQLString)},
test: {type: GraphQLString},
// FIXME: USER TYPES!
firstName: {type: GraphQLString},
lastName: {type: GraphQLString},
email: {type: GraphQLString},
age: {type: GraphQLInt},
token: {type: GraphQLString},
image: {type: GraphQLString},
phone: {type: GraphQLInt},
address: {type: GraphQLString},
// ! TYPE RELATION
userRelatedToEvent: {
type: UserType,
resolve: async (parent, args) => {
const id = {
id: parent.organizerId
};
return await getCurrentUser(id);
}
}
})
})
};
this is my code on UserType
that is doing a Type relation on EventType
see resolve
prop
module.exports = {
UserType: new GraphQLObjectType({
name: 'User',
fields: () => ({
id: {type: GraphQLID},
firstName: {type: GraphQLString},
lastName: {type: GraphQLString},
email: {type: GraphQLString},
age: {type: GraphQLInt},
token: {type: GraphQLString},
image: {type: GraphQLString},
phone: {type: GraphQLInt},
address: {type: GraphQLString},
eventsId: {type: new GraphQLList(GraphQLID)}, // <-- this will be the query for userRelatedToUser
statusCode: {type: GraphQLInt},
isSuccess: {type: GraphQLBoolean},
msg: {type: GraphQLString},
test: {type: GraphQLString}, // testing query
// errors: { type: GraphQ LString },
eventRelatedToUser: {
type: require('./eventTypeDef').EventType,
resolve: async (parent, args) => {
const event = {
event: parent.eventsId
};
const test = await getEventWithEventId(event); // <-- queries event
const id = {
id: test.attendeesId
};
const users = await getCurrentUser(id); // <-- user info
console.log(users);
return {...users, ...test};
}
}
})
})
};
graphql
So I have a graphQL userType
and EventType
and on my userType
I have to make a nested query using EventType
but I also need to return user information but since EventType
doesn't have fields same as user info so to pass my code on testing I had to manually copy-paste UserType
to EventType
but I dont want this to solution. is there a way to like merge types? I'm using mongoDB as my database so when trying to do a POPULATE
the user info comes back but Graphql making it hard. sorry im still new to graphql.
so this is what I did on EventType
to pass the test. under fixme i copy the UserType
module.exports = {
EventType: new GraphQLObjectType({
name: 'Event',
fields: () => ({
id: {type: GraphQLID},
organizerId: {type: new GraphQLList(GraphQLID)},
title: {type: GraphQLString},
image: {type: GraphQLString},
description: {type: GraphQLString},
location: {type: GraphQLString},
items: {type: GraphQLInt},
date: {type: GraphQLString},
attendeesId: {type: new GraphQLList(GraphQLID)},
supplies: {type: new GraphQLList(GraphQLString)},
test: {type: GraphQLString},
// FIXME: USER TYPES!
firstName: {type: GraphQLString},
lastName: {type: GraphQLString},
email: {type: GraphQLString},
age: {type: GraphQLInt},
token: {type: GraphQLString},
image: {type: GraphQLString},
phone: {type: GraphQLInt},
address: {type: GraphQLString},
// ! TYPE RELATION
userRelatedToEvent: {
type: UserType,
resolve: async (parent, args) => {
const id = {
id: parent.organizerId
};
return await getCurrentUser(id);
}
}
})
})
};
this is my code on UserType
that is doing a Type relation on EventType
see resolve
prop
module.exports = {
UserType: new GraphQLObjectType({
name: 'User',
fields: () => ({
id: {type: GraphQLID},
firstName: {type: GraphQLString},
lastName: {type: GraphQLString},
email: {type: GraphQLString},
age: {type: GraphQLInt},
token: {type: GraphQLString},
image: {type: GraphQLString},
phone: {type: GraphQLInt},
address: {type: GraphQLString},
eventsId: {type: new GraphQLList(GraphQLID)}, // <-- this will be the query for userRelatedToUser
statusCode: {type: GraphQLInt},
isSuccess: {type: GraphQLBoolean},
msg: {type: GraphQLString},
test: {type: GraphQLString}, // testing query
// errors: { type: GraphQ LString },
eventRelatedToUser: {
type: require('./eventTypeDef').EventType,
resolve: async (parent, args) => {
const event = {
event: parent.eventsId
};
const test = await getEventWithEventId(event); // <-- queries event
const id = {
id: test.attendeesId
};
const users = await getCurrentUser(id); // <-- user info
console.log(users);
return {...users, ...test};
}
}
})
})
};
graphql
graphql
edited Jan 3 at 18:33
David Maze
15.7k31531
15.7k31531
asked Jan 3 at 15:05
artooartoo
359
359
Does an event itself have a first name, last name, etc., or are those fields copies of the fields on the associated user?
– David Maze
Jan 3 at 18:36
those are copies. thats what im trying to figure out. to associate both fields. is there such way? @DavidMaze
– artoo
Jan 3 at 19:35
The ordinary GraphQL way would be to not have those fields directly; a query likesomeEvent { user { firstName } }
would retrieve it.
– David Maze
Jan 3 at 22:49
add a comment |
Does an event itself have a first name, last name, etc., or are those fields copies of the fields on the associated user?
– David Maze
Jan 3 at 18:36
those are copies. thats what im trying to figure out. to associate both fields. is there such way? @DavidMaze
– artoo
Jan 3 at 19:35
The ordinary GraphQL way would be to not have those fields directly; a query likesomeEvent { user { firstName } }
would retrieve it.
– David Maze
Jan 3 at 22:49
Does an event itself have a first name, last name, etc., or are those fields copies of the fields on the associated user?
– David Maze
Jan 3 at 18:36
Does an event itself have a first name, last name, etc., or are those fields copies of the fields on the associated user?
– David Maze
Jan 3 at 18:36
those are copies. thats what im trying to figure out. to associate both fields. is there such way? @DavidMaze
– artoo
Jan 3 at 19:35
those are copies. thats what im trying to figure out. to associate both fields. is there such way? @DavidMaze
– artoo
Jan 3 at 19:35
The ordinary GraphQL way would be to not have those fields directly; a query like
someEvent { user { firstName } }
would retrieve it.– David Maze
Jan 3 at 22:49
The ordinary GraphQL way would be to not have those fields directly; a query like
someEvent { user { firstName } }
would retrieve it.– David Maze
Jan 3 at 22:49
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54024886%2fquery-with-different-types-in-graphql%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54024886%2fquery-with-different-types-in-graphql%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Does an event itself have a first name, last name, etc., or are those fields copies of the fields on the associated user?
– David Maze
Jan 3 at 18:36
those are copies. thats what im trying to figure out. to associate both fields. is there such way? @DavidMaze
– artoo
Jan 3 at 19:35
The ordinary GraphQL way would be to not have those fields directly; a query like
someEvent { user { firstName } }
would retrieve it.– David Maze
Jan 3 at 22:49