How to post model with hasMany relation - has-many

I'm setting up an api using LoopBack 4 and its mysql connector. So I have a model Plane, which has a #hasMany relation with pilots :
class Plane extends Entity {
#property({
id: true,"dataPrecision": 10, "dataScale": 0, "nullable": "N" },
})
id: number;
#property()
name: string;
#hasMany(() => Pilot, { keyTo: 'planeId' })
pilots?: Array<Pilot>;
So now what I wanted to do is to create a plane and add it its pilots on a single request. In my plane repository I made something like this :
class PlaneRepository extends DefaultCrudRepository<...> {
planes;
constructor(
#inject('datasources') dataSource: any,
#repository.getter('PilotRepository') getPilotRepository
) {
this.planes = this.createHasManyRepositoryFactoryFor('planes', getIpilotRepository);
}
And my controller looks like that:
class PlaneController {
#post('/project', {
responses: {
'200': {
description: 'Plane model instance',
content: { 'application/json': { schema: getModelSchemaRef(Project) }
},
},
})
async create(
#requestBody({
content: {
'application/json': {
schema: getModelSchemaRef(Plane, { exclude: ['id'], includeRelations: true }),
},
},
})
plane: Omit<Plane, 'id'>,
): Promise<plane> {
return this.planeRepository.create(plane);
}
}
But when I try to call my route with something like that
{ name: 'Jet 27', pilots: [ { id: 0, name: Chuck Berry } ] }
I have a 422 error:
"The Plane instance is not valid. Details: pilots is not defined
in the model (value: undefined)."
I don't know if this is the expected behaviour, I must admit that I'm a bit confused with the relations way of functionning, but if it is, then how am I supposed to do.

When creating a model like your plane from the example above, it is not intended to pass navigational properties like pilots in the requestBody. There is even a recently landed feature which rejects such requests as is can not be handled.
If you really want to handle such complex requests, e.g to save multiple client requests to different endpoints, you can implement such behaviour directly in your controller class, but you have to remove the navigational property from the object the planeRepository gets passed (as this would throw an exception).
Example (not tested):
#post('/project', {
responses: {
'200': {
description: 'Plane model instance',
content: { 'application/json': { schema: getModelSchemaRef(Project) }
},
},
})
async create(
#requestBody({
content: {
'application/json': {
schema: getModelSchemaRef(PlaneWithRelations, { exclude: ['id'] }),
},
},
})
plane: Omit<PlaneWithRelations, 'id'>,
): Promise<Plane> {
const plane = await this.planeRepository.create({ name: plane.name });
for (const pilot of planes.pilots) {
await this.pilotRepository.create({ ...pilot, planeId: plane.id });
}
return plane;
}
It should also be possible to group the changes in a database transaction.

Related

how do I shape my graphql backend response?

I am querying my graphql backend and need the response to have a flatten shape,
my query:
gql`
{
questions {
edges {
id
title
author: user {
email
}
}
}
}
`
my response:
'5d3eafb7889a135ff8cd950c': {
id: '5d3eafb7889a135ff8cd950c',
title: 's',
author: {
email: 'dggdfgdgfd#gmail.com',
__typename: 'User'
},
__typename: 'Question'
},
problem is with author, I need as a string instead of an object:
id: '5d3eafb7889a135ff8cd950c',
title: 's',
author: 'dggdfgdgfd#gmail.com' // <===
`
You have two options:
1) After getting the data from the GraphQL server, transform the data as you need to fit your needs. A Javascript implementation might be:
function flattenGqlResponse (response) {
return Object.keys(response).map((question) => {
return {
id: response[question].id,
title: response[question].title,
author: response[question].author.email
}
}
);
}
2) Flatten the structure in your GraphQL Resolver on your GraphQL Server. Likely, you do not want to pursue this route based on what I see about your schema, unless your User object only has one field, that is email

I have confusion on relay and graphql resolve method

Apologies if this is a stupid question. this is the code for relay/graphql pagination that's confusing me:
const GraphQLTodo = new GraphQLObjectType({
name: 'Todo',
fields: {
id: globalIdField('Todo'),
text: {
type: GraphQLString,
resolve: (obj) => obj.text,
},
complete: {
type: GraphQLBoolean,
resolve: (obj) => obj.complete,
},
},
interfaces: [nodeInterface],
});
/* When pagination is needed, make a connection */
const {
connectionType: TodosConnection,
edgeType: GraphQLTodoEdge,
} = connectionDefinitions({
name: 'Todo',
nodeType: GraphQLTodo,
});
const GraphQLUser = new GraphQLObjectType({
name: 'User',
fields: {
id: globalIdField('User'),
todos: {
type: TodosConnection,
args: {
status: {
type: GraphQLString,
defaultValue: 'any',
},
...connectionArgs,
},
resolve: (obj, {status, ...args}) =>
connectionFromArray(getTodos(status), args),
},
totalCount: {
type: GraphQLInt,
resolve: () => getTodos().length,
},
completedCount: {
type: GraphQLInt,
resolve: () => getTodos('completed').length,
},
},
interfaces: [nodeInterface],
});
const Root = new GraphQLObjectType({
name: 'Root',
fields: {
viewer: {
type: GraphQLUser,
resolve: () => getViewer(),
},
node: nodeField,
},
});
You can see that on the GraphQLTodo field, it has text and complete fields with resolve function passed an obj parameter, how is obj passed there? is it from GraphQLUser resolve? I've read on docs that source(in this case obj) - The object resolved from the field on the parent type. is it not from the root query? how is obj here created?
The Connection
Here is where (some of) the magic happens:
const {
connectionType: TodosConnection,
edgeType: GraphQLTodoEdge,
} = connectionDefinitions({
name: 'Todo',
nodeType: GraphQLTodo,
});
You have now told GraphQL that a TodosConnection is going to be made up of GraphQLTodo nodes. Now, let's take a look at where the objects are actually fetched for the connection in your GraphQLUser object, which is on the todos field:
todos: {
type: TodosConnection,
args: {
status: {
type: GraphQLString,
defaultValue: 'any',
},
...connectionArgs,
},
resolve: (obj, {status, ...args}) =>
connectionFromArray(getTodos(status), args),
},
So where does the object come from? The key part here is the getTodos function, which is responsible for actually getting an array of the objects from your data source. Since this field is a TodosConnection and we've already specified in the connection definitions that the nodes are GraphQLTodos, GraphQL knows that the text and complete fields are resolved by getting (in this case) identically named fields on the objects that have been returned. In other words, the returned object is passed to the resolve method on each field.
Querying the Root
You have two fields exposed on Root: viewer and node. Ignoring node for a moment, you have just one way to actually query todos. Since viewer is of type GraphQLUser, and GraphQLUser has that todos field, they can be fetched only as a subfield of viewer, like this:
{
viewer {
todos(first: 10) {
edges {
# each node is a Todo item
node {
text
complete
}
}
}
}
}
Mystery of the Node
But what about that node field? Relay wants to be able to fetch any object using a top-level query, i.e. on your Root field, when given a unique globalId, which is just a base64 encoding of the type name and the id, so Todo:1 is encoded to VG9kbzox. This is set up in the nodeDefinitions (which you haven't included here, but probably have). In those definitions, the globalId is parsed back into the type (Todo) and id (1), and once again you then tell it how to fetch the correct object from your data source. It might look something like:
const { nodeInterface, nodeField } = nodeDefinitions(
(globalId) => {
const { type, id } = fromGlobalId(globalId);
if (type === 'Todo') {
return getTodo(id)
} else if (type === 'User') {
return getUser(id)
}
...
Because you're implementing the nodeInterface in both your GraphQLTodo and GraphQLUser types, Relay will be able query for either of them from the Root's node field.

Each Node of data perform separate database request

below is the GraphQLObject Fields
userId: {
type: GraphQLID,
resolve: obj => {
console.log(obj._id);
return obj._id;
}
},
email: { type: GraphQLString },
password: { type: GraphQLString },
firstName: { type: GraphQLString },
lastName: { type: GraphQLString },
mine server sents multiple request equally as of mine documents, here it will send 5 different request.
how can i optimize these request get all data in one request
589800cf39b58b29c4de90dd
--------------------------------
58980673e7c9a733009091d1
--------------------------------
58985339651c4a266848be42
--------------------------------
589aac5f884b062b979389bc
--------------------------------
589aad9d24989c2d50f2a25a
In such a case you could create a query method which would accept an array as a parameter, which would be an array of IDs in this case.
getUsers: {
type: new GraphQLList(User),
args: {
ids: {
type: new GraphQLNonNull(new GraphQLList(new GraphQLNonNull(GraphQLID)))
}
},
resolve: (root, args, context) => {
let query = 'SELECT * FROM users WHERE id = ANY($1)';
return pgClient.query(query, [args.ids], (err, result) => {
// here you would access all returned rows in the result object
console.log(result);
});
}
}
The query variable would differ depending on what database you are using. In this example I have used the node-postgres module for PostgreSQL. However, the concept is the same - use array of ids to perform single query returning all users.
And then you could call that query:
query getUsers($ids: [ID!]!) {
getUsers(ids: $ids){
id
email
...
}
}
// and the variables below
{
ids: ['id#1', 'id#2', 'id#3']
}
This is a job for Dataloader, a library from Facebook specifically for batching together queries like this:
https://github.com/facebook/dataloader

GraphQL pass args to sub resolve

I have a relationship between User and Post. This is how I query the User Posts.
const UserType = new GraphQLObjectType({
name: 'User'
fields: () => ({
name: {
type: GraphQLString
},
posts: {
type: new GraphQLList(PostType),
resolve(parent, args , { db }) {
// I want to get here the args.someBooleanArg
return someLogicToGetUserPosts();
}
}
})
});
The main query is:
const queryType = new GraphQLObjectType({
name: 'RootQuery',
fields: {
users: {
type: new GraphQLList(UserType),
args: {
id: {
type: GraphQLInt
},
someBooleanArg: {
type: GraphQLInt
}
},
resolve: (root, { id, someBooleanArg }, { db }) => {
return someLogicToGetUsers();
}
}
}
});
The problem is the args in the resolve function of the UserType posts is empty object, how do i pass the args from the main query to sub resolves functions?
When resolving the root query you can use object assign to attach the argument to the user object returned.
Then, on the user type, resolve the argument from the root value (first argument of resolve function).
Example:
const queryType = new GraphQLObjectType({
name: 'RootQuery',
fields: {
users: {
type: new GraphQLList(UserType),
args: {
id: {
type: GraphQLInt
},
someBooleanArg: {
type: GraphQLInt
}
},
resolve: (root, { id, someBooleanArg }, { db }) => {
return Promise.resolve(someLogicToGetUsers()).then(v => {
return Object.assign({}, v, {
someBooleanArg
});
});
}
}
}
});
const UserType = new GraphQLObjectType({
name: 'User'
fields: () => ({
name: {
type: GraphQLString
},
posts: {
type: new GraphQLList(PostType),
resolve(parent, args , { db }) {
console.log(parent.someBooleanArg);
return someLogicToGetUserPosts();
}
}
})
});
You can use the resolver fouth argument, info, to receive the desired variable - from Apollo docs:
Every resolver in a GraphQL.js schema accepts four positional arguments:
fieldName(obj, args, context, info)
{ result }
These arguments have
the following meanings and conventional names:
obj: The object that contains the result returned from the resolver on
the parent field, or, in the case of a top-level Query field, the
rootValue passed from the server configuration. This argument enables
the nested nature of GraphQL queries.
args: An object with the
arguments passed into the field in the query. For example, if the
field was called with author(name: "Ada"), the args object would be: {
"name": "Ada" }.
context: This is an object shared by all resolvers in
a particular query, and is used to contain per-request state,
including authentication information, dataloader instances, and
anything else that should be taken into account when resolving the
query. If you're using Apollo Server, read about how to set the
context in the setup documentation.
info: This argument should only be
used in advanced cases, but it contains information about the
execution state of the query, including the field name, path to the
field from the root, and more. It's only documented in the GraphQL.js
source code.
The info seems to be a very undocumented feature, but I'm using it now with no problems (at least until somebody decide to change it).
Here is the trick:
const UserType = new GraphQLObjectType({
name: 'User'
fields: () => ({
name: {
type: GraphQLString
},
posts: {
type: new GraphQLList(PostType),
resolve(parent, args , { db }, info) {
// I want to get here the args.someBooleanArg
console.log("BINGO!");
console.log(info.variableValues.someBooleanArg);
return someLogicToGetUserPosts();
}
}
})
});

Relayjs Graphql user authentication

Is it possible to authenticate users with different roles solely trough a graphql server in combination with relay & react?
I looked around, and couldn't find much info about this topic.
In my current setup, the login features with different roles, are still going trough a traditional REST API... ('secured' with json web tokens).
I did it in one of my app, basically you just need a User Interface, this one return null on the first root query if nobody is logged in, and you can then update it with a login mutation passing in the credentials.
The main problem is to get cookies or session inside the post relay request since it does'nt handle the cookie field in the request.
Here is my client mutation:
export default class LoginMutation extends Relay.Mutation {
static fragments = {
user: () => Relay.QL`
fragment on User {
id,
mail
}
`,
};
getMutation() {
return Relay.QL`mutation{Login}`;
}
getVariables() {
return {
mail: this.props.credentials.pseudo,
password: this.props.credentials.password,
};
}
getConfigs() {
return [{
type: 'FIELDS_CHANGE',
fieldIDs: {
user: this.props.user.id,
}
}];
}
getOptimisticResponse() {
return {
mail: this.props.credentials.pseudo,
};
}
getFatQuery() {
return Relay.QL`
fragment on LoginPayload {
user {
userID,
mail
}
}
`;
}
}
and here is my schema side mutation
var LoginMutation = mutationWithClientMutationId({
name: 'Login',
inputFields: {
mail: {
type: new GraphQLNonNull(GraphQLString)
},
password: {
type: new GraphQLNonNull(GraphQLString)
}
},
outputFields: {
user: {
type: GraphQLUser,
resolve: (newUser) => newUser
}
},
mutateAndGetPayload: (credentials, {
rootValue
}) => co(function*() {
var newUser = yield getUserByCredentials(credentials, rootValue);
console.log('schema:loginmutation');
delete newUser.id;
return newUser;
})
});
to keep my users logged through page refresh I send my own request and fill it with a cookie field... This is for now the only way to make it work...

Resources