with appolo server 2 beta,
I have a resolver like:
Travaux: new GraphQLEnumType({
name: 'Travaux',
values: {
ins: { value: 'en instruction' },
val: { value: 'valide' },
ech: { value: 'échu' }
}
})
and a Schema
gql`
type Query {
titre(id: String!): Titre
}
type Titre {
travaux: Travaux
}
enum Travaux {
ins
val
ech
}
`
This makes an error:
Travaux.name was defined in resolvers, but enum is not in schema
If I remove the Travaux resolver, it works…
What is arong here?
The resolver has to be:
Travaux = {
ins: 'en instruction',
val: 'valide',
ech: 'échu'
}
Related
I have an array of entities that look like this:
const aEntities = [
{
id: 1,
name: 'Test',
oneToManyRelation: [
{
id: 2
},
{
id: 3
}
],
oneToOneRelation: {
id: 1
}
}
];
The entities are represented by the type AType. I want to make an extension of this type in a separate subschema and prove that it is possible to add fields that derive their values from the contents of oneToOneRelation and oneToManyRelation respectively.
The following schema, implementing a derived field based on oneToOneRelation, works fine:
const aSchema = makeExecutableSchema({
resolvers: {
Query: {
aEntities: () => aEntities
}
},
schemaTransforms: [stitchingDirectivesValidator],
typeDefs: gql`
${allStitchingDirectivesTypeDefs}
type AType {
id: ID!
name: String!
oneToOneRelation: AEmbeddedType!
}
type AEmbeddedType {
id: ID!
}
type Query {
aEntities: [AType!]!
}
`
});
const bSchema = makeExecutableSchema({
resolvers: {
AType: {
oneToOneId: ({ oneToOneRelation }) => oneToOneRelation.id
},
Query: {
aEntities_fromBSchema: (_, { keys }) => keys,
}
},
schemaTransforms: [stitchingDirectivesValidator],
typeDefs: gql`
${allStitchingDirectivesTypeDefs}
type AType #key(selectionSet: "{ oneToOneRelation { id } }") {
oneToOneId: String!
}
scalar Key
type Query {
aEntities_fromBSchema(keys: [Key!]!): [AType!]! #merge
}
`
})
const schema = stitchSchemas({
subschemaConfigTransforms: [stitchingDirectivesTransformer],
subschemas: [
{
schema: aSchema
},
{
schema: bSchema,
}
]
})
But once I add oneToManyRelation { id } to the selectionSet i run into problems:
const aSchema = makeExecutableSchema({
resolvers: {
Query: {
aEntities: () => aEntities
}
},
schemaTransforms: [stitchingDirectivesValidator],
typeDefs: gql`
${allStitchingDirectivesTypeDefs}
type AType {
id: ID!
name: String!
oneToManyRelation: [AEmbeddedType!]!
oneToOneRelation: AEmbeddedType!
}
type AEmbeddedType {
id: ID!
}
type Query {
aEntities: [AType!]!
}
`
});
const bSchema = makeExecutableSchema({
resolvers: {
AType: {
oneToManyIds: ({ oneToManyRelation }) => oneToManyRelation.map(({ id }) => id),
oneToOneId: ({ oneToOneRelation }) => oneToOneRelation.id
},
Query: {
aEntities_fromBSchema: (_, { keys }) => keys,
}
},
schemaTransforms: [stitchingDirectivesValidator],
typeDefs: gql`
${allStitchingDirectivesTypeDefs}
type AType #key(selectionSet: "{ oneToOneRelation { id }, oneToManyRelation { id } }") {
oneToOneId: String!
oneToManyIds: [String!]!
}
scalar Key
type Query {
aEntities_fromBSchema(keys: [Key!]!): [AType!]! #merge
}
`
})
I get the following error:
oneToManyRelation.map is not a function
And when I log the keys parameter in the aEntities_fromBSchema resolver it seems that oneToManyRelation haven't been resolved to be an array at all, but rather an (empty) object:
[
{
oneToOneRelation: [Object: null prototype] { id: '1' },
oneToManyRelation: [Object: null prototype] { id: undefined },
__typename: 'AType'
}
]
Is referencing list types in key selection sets simply forbidden as of graphql-tools v 7.0.2? It looks like I actually can circumvent the issue by using a subschema merge config defined outside of the SDL (without batching, instead using the args and selectionSet config parameters), but for validation/gateway reasons I'd prefer to have all my subschemas contain all of their type merging instructions as SDL directives.
Nb. This is a simplified representation of a real world problem.
Nb2. In the real world application one of my subschemas is a remote GraphQL application that I don't control, hence the need for some advanced tailoring in the stitching layer.
Edit: Simply adding the following to the merge options on the subschema config seems to solve the problem. Someone know of a good reason why this doesn't seem to be reproducible with SDL directives? (Or a good way to do so?)
// AType
{
argsFromKeys: (keys) => ({ keys }),
fieldName: 'aEntities_fromBSchema',
key: ({ oneToOneRelation, oneToManyRelation }) => ({ oneToManyRelation, oneToOneRelation }),
selectionSet: '{ oneToOneRelation { id }, oneToManyRelation { id } }'
}
You have likely found a bug! Please open an issue on the GitHub repo so we can track it. :)
I would like to know how can I pass an argument in "child" type.
With the request, I would like to have only the message with id 1 from user id 1.
Currently I have this request :
{user(id:1){
email,
username,
messages(id:1){
text,
id
}
}}
I modified in the schema the User type, and changed
messages: [Message]
by
messages(id: Int): [Message]
But I have always all messages from user and not the message with specific id.
schema.js
import { gql } from 'apollo-server';
export const typeDefs = gql`
# declare custom scalars
scalar Date
##########
## TYPE ##
##########
# a group
type Group {
id: Int!
name: String
users: [User]!
messages: [Message]
}
# a user
type User {
id: Int! # unique id for the user
email: String!
username: String
messages(id: Int): [Message]
groups: [Group]
friends: [User]
}
# a message sent from a user to a group
type Message {
id: Int!
to: Group!
from: User!
text: String!
createdAt: Date!
}
###########
## QUERY ##
###########
# query for types
type Query {
user(email: String, id: Int): User
messages(groupId: Int, userId: Int): [Message]
}
schema {
query: Query
}
`;
export default typeDefs;
resolvers.js
import GraphQLDate from 'graphql-date';
import { Group, Message, User } from './connectors';
export const resolvers = {
Date: GraphQLDate,
Query: {
group(_, args) {
return Group.find({ where: args });
},
messages(_, args) {
return Message.findAll({
where: args,
order: [['createdAt', 'DESC']],
});
},
user(_, args) {
return User.findOne({ where: args });
},
},
Group: {
users(group) {
return group.getUsers();
},
messages(group) {
return Message.findAll({
where: { groupId: group.id },
order: [['createdAt', 'DESC']],
});
},
},
Message: {
to(message) {
return message.getGroup();
},
from(message) {
return message.getUser();
},
},
User: {
messages(user) {
return Message.findAll({
where: { userId: user.id },
order: [['createdAt', 'DESC']],
});
},
groups(user) {
return user.getGroups();
},
friends(user) {
return user.getFriends();
},
},
};
export default resolvers;
You have to update User.messages resolver. This way:
User: {
messages(user, { id }) {
return Message.findAll({
where: { userId: user.id, id },
order: [['createdAt', 'DESC']],
});
},
groups(user) {
return user.getGroups();
},
friends(user) {
return user.getFriends();
},
},
If you want to make id parameter optional, something like this may help:
messages(user, { id }) {
const where = { userId: user.id }
if (id) {
where.id = id
}
return Message.findAll({
where,
order: [['createdAt', 'DESC']],
});
},
Im trying to return a string with React and GraphQL but I'm getting stuck at the first stage. Here is my attempt:
import { makeExecutableSchema } from 'graphql-tools';
const typeDefs = `
type Query {
author: Person
}
type Person {
name: String
}
`;
const resolvers = {
Query: {
author: { name: 'billy' },
},
};
const schema = makeExecutableSchema({
typeDefs,
resolvers,
});
createApolloServer({ schema });
And this is my understanding of that code:
In my schema I've defined a Query called author which should return a Person.
A Person has a name field which is a string.
My resolver has a Query called author which should return an object with a name field of value 'billy'
However in my Graphicool browser tools this query:
query {
author{
name
}
}
Returns this:
{
"data": {
"author": null
}
}
Resolvers are functions which GraphQL will call when resolving that particular field. That means your resolvers object should look more like this:
const resolvers = {
Query: {
author: () => ({ name: 'billy' }),
},
}
Or, alternatively,
const resolvers = {
Query: {
author() {
return { name: 'billy' }
},
},
}
You can check out the docs for more information.
import { createApolloServer } from 'meteor/apollo';
import { makeExecutableSchema } from 'graphql-tools';
import merge from 'lodash/merge'; // will be useful later when their are more schemas
import GroupsSchema from './Groups.graphql';
import GroupsResolvers from './resolvers';
const typeDefs = [GroupsSchema];
const resolvers = merge(GroupsResolvers);
const schema = makeExecutableSchema({
typeDefs,
resolvers,
});
createApolloServer({ schema });
In ./Groups.graphql:
type Query {
hi: String
groups: [Group]
group: Group
}
type Group {
name: String
}
In './resolvers':
export default {
Query: {
hi() {
return 'howdy';
},
groups() {
return [{ name: 'one', _id: '123' }, { name: 'two', _id: '456' }];
// return Groups.find().fetch();
},
group() {
return { name: 'found me' };
},
},
};
In a React component:
const mainQuery = gql`
{
groups {
name
}
}
`;
export default graphql(mainQuery)(ComponentName);
Im using Apollo 2. With the GraphiQL interface I can run a query which returns all groups fine. However when I try to pass a name to find just 1 group it doenst work.
Schema
type Query {
hi: String
groups: [Group]
group(name: String): Group
}
type Group {
name: String
}
Resolvers:
Query: {
hi() {
return 'howdy';
},
groups() {
return Groups.find().fetch();
},
group(one, two, three) {
console.log('group resolver called');
console.log(one, two, three);
//return Groups.findOne(two.name);
},
},
This is my GraphiQL groups query which works fine:
{
groups {
name
}
}
But my group query returns an error:
{
group(name: "some name") {
name
}
}
{
"errors": [
{
"message": "Unknown argument \"name\" on field \"group\" of type \"Query\".",
"locations": [
{
"line": 2,
"column": 9
}
]
}
]
}
UPDATE - This is my full file:
import { createApolloServer } from 'meteor/apollo';
import { makeExecutableSchema } from 'graphql-tools';
import merge from 'lodash/merge';
import GroupsSchema from '../../api/groups/Groups.graphql';
import GroupsResolvers from '../../api/groups/resolvers';
const typeDefs = [GroupsSchema];
const resolvers = merge(GroupsResolvers);
const schema = makeExecutableSchema({
typeDefs,
resolvers,
});
createApolloServer({ schema });
In resolvers.js
import Groups from './groups';
export default {
Query: {
hi() {
return 'howdy';
},
groups() {
return [{ name: '1', test: 'test 1' }, { name: '2', test: 'test 2' }];
// return Groups.find().fetch();
},
group(one, two, three) {
console.log('group resolver called');
console.log(one, two, three);
// return Groups.findOne('Apon9HKE9MeZqeXsZ');
},
},
};
In Groups.graphql
type Query {
hi: String
groups: [Group]
group(name: String): Group
}
type Group {
name: String
}
I think your typeDefs and resolvers are wrongs, try:
typeDefs
const typeDefs = `
type Query {
hi: String
groups: [Group]
group(name: String): Group
}
type Group {
name: String
}`;
Resolvers
export default {
Query: {
hi() {
return 'howdy';
},
groups() {
return [{ name: '1', test: 'test 1' }, { name: '2', test: 'test 2' }];
// return Groups.find().fetch();
},
group(_, { name }) {
console.log('group resolver called');
console.log(one, two, three);
// return Groups.findOne('Apon9HKE9MeZqeXsZ');
},
},
};
I have a nested javascript class (see below) that is exposed through my GraphQL server. Can I write a GQL schema that exposes this complex structure as a single object? (aka flattened).
The Nested Object
interface Promotion {
id
type
data: PromotionType1 | PromotionType2
}
interface PromotionType1 {
a
b
}
interface PromotionType2 {
c
d
}
The desired GQL query to access the Object
I want to write a GQL schema so that I can query this object as follows:
promotion(id: "123") {
id
type
... on PromotionType1 {
a
b
}
... on PromotionType2 {
c
d
}
}
Is this possible with GQL?
You can use GraphQLUnitType and GraphQLInterfaceType to be able to make that GraphQL query to access the object, if you restructure your nested object. It seems you intended to use inheritance while designing promotion types and ended up having the subtypes as a field in parent type. Instead the structure should be like:
interface Promotion {
id
type
}
interface PromotionType1 extends Promotion {
a
b
}
interface PromotionType2 extends Promotion {
c
d
}
Promotion is the base type. We can have it as GraphQLInterfaceType:
const PromotionType = new GraphQLInterfaceType({
name: 'PromotionInterface',
fields: {
id: { type: GraphQLID },
type: { type: GraphQLString }
}
});
You need instances of PromotionType1 and PromotionType2. So, these can be GraphQLObjectType.
const PromotionType1 = new GraphQLObjectType({
name: 'PromotionType1',
interfaces: [ PromotionType ],
fields: {
id: { type: GraphQLID },
type: { type: GraphQLString },
a: { type: GraphQLString },
b: { type: GraphQLString },
},
isTypeOf: value => value instanceof PromotionType1
});
const PromotionType2 = new GraphQLObjectType({
name: 'PromotionType2',
interfaces: [ PromotionType ],
fields: {
id: { type: GraphQLID },
type: { type: GraphQLString },
c: { type: GraphQLString },
d: { type: GraphQLString },
},
isTypeOf: value => value instanceof PromotionType2
});
If your have JS class Promotion1 for GraphQL type PromotionType1 and Promotion2 for PromotionType2, the GraphQLObjectType for exposing promotion data will be like:
var Promotion = new GraphQLUnionType({
name: 'Promotion',
types: [ PromotionType1, PromotionType2 ],
resolveType(value) {
if (value instanceof Promotion1) {
return PromotionType1;
}
if (value instanceof Promotion2) {
return PromotionType2;
}
}
});
You can then query promotion data with:
promotion(id: "123") {
id,
type,
... on PromotionType1 {
a,
b,
}
... on PromotionType2 {
c,
d,
}
}
You can check out this example.
One possible solution is to flatten the object structure in your resolver. This would avoid the need to do anything complex in your GQL schema.