GraphQL - Model(s) belong to specific user field - laravel

I'm using Lighthouse package to implement GraphQL, the users in my app belong to an entity and I need to get models that belong to the entity of each user, my schema is like this for the moment
"A date string with format `Y-m-d`, e.g. `2011-05-23`."
scalar Date #scalar(class: "Nuwave\\Lighthouse\\Schema\\Types\\Scalars\\Date")
"A datetime string with format `Y-m-d H:i:s`, e.g. `2018-05-23 13:43:32`."
scalar DateTime #scalar(class: "Nuwave\\Lighthouse\\Schema\\Types\\Scalars\\DateTime")
type Query {
me: User #auth
execution: [RobotTime!]! #all
}
type RobotTime {
id: ID!
instance: Instance!
robot: Robot!
start: DateTime!
end: DateTime!
}
type Instance {
id: ID!
name: String!
token: String!
}
type Robot {
id: ID!
start_bot: String!
process: Process!
}
type Process {
id: ID!
name: String!
token: String!
}
type User {
id: ID!
name: String!
email: String!
created_at: String!
updated_at: String
}
I have searched the documentation, but I can't find anything that helps me to do what I need to do.
Currently I have it done in a controller and I return it as a json with a not so complex logic, there are about 5 models that I use.
Sorry for my bad English

You need to declare the appropriate relation in your User model in laravel, sth like this:
public function entity(): BelongsTo
{
return $this->belongsTo(Entity::class);
}
Then add the relation in User type in graphql:
type User {
id: ID!
name: String!
email: String!
created_at: DateTime!
updated_at: DateTime
entity: Entity #belongsTo
}
Of course, you'll need to declare an Entity model in laravel and type in graphql.

Related

Is it possible to create two or more relations to a model in prisma?

I'm trying to create two relations to a model in datamodel.prisma
datamodel.prisma
type User {
id: ID! #id
user_id: String! #unique
username: String!
email: String! #unique
}
type Operation {
id: ID! #id
teams: [User] #relation(link: INLINE)
created_by: User #relation(link: INLINE)
}
When I try to deploy this is the error I'm getting
Error
Errors:
Operation
✖ The relation field `teams` must specify a `#relation` directive: `#relation(name: "MyRelation")`
✖ The relation field `created_by` must specify a `#relation` directive: `#relation(name: "MyRelation")`
What I want to achieve is an operation can have multiple members(one to many) and can only be created by one member(one to one). How can I achieve this in Prisma?
Could you try creating it like this:
type User {
id: ID! #id
user_id: String! #unique
username: String!
email: String! #unique
}
type Operation {
id: ID! #id
teams: [User] #relation(name: "Teams", link: TABLE)
created_by: User #relation(name: "Createdby", link: TABLE)
}
The name field is required while creating multiple relations to the same model.
Also I'm assuming you are using Postgres.

Declare variable in lighthouse graphql files

I've created a graphql file like this:
type Field {
id: ID!
name_en: String!
name_fa: String!
description: String!
img_url: String!
created_at: DateTime!
updated_at: DateTime!
subFields: [SubField] #hasMany
}
extend type Query {
fields(input: ListInput #spread): [Field] #paginate(model: "App\\Models\\CustomerManagement\\BusinessInformation\\Field" defaultCount: 10)
field(id: ID! #eq): Field #find(model: "App\\Models\\CustomerManagement\\BusinessInformation\\Field")
}
extend type Mutation {
createField(
name_en: String! #rules(apply: ["required"])
name_fa: String
description: String
img_url: String
): Field #create(model: "App\\Models\\CustomerManagement\\BusinessInformation\\Field")
updateField(
id: ID! #rules(apply: ["required", "int"])
name_en: String #rules(apply: ["required"])
name_fa: String
description: String
img_url: String
): Field #update(model: "App\\Models\\CustomerManagement\\BusinessInformation\\Field")
deleteField(
id: ID! #rules(apply: ["required", "int"])
): Field #delete(model: "App\\Models\\CustomerManagement\\BusinessInformation\\Field")
}
Every time I want to refer to Field model, I have to type the whole App\\Models\\CustomerManagement\\BusinessInformation\\Field. I was wondering if I could declare a variable like model_namespace and use it instead of the big model namespace.
EDIT:
https://lighthouse-php.com/4.4/api-reference/directives.html#namespace
You should use the #namespace directive
extend type Query #namespace(field: "App\\Blog") {
posts: [Post!]! #field(resolver: "Post#resolveAll")
}
You have a couple of options:
You can basically load a default namespace array using namespace configuration:
https://github.com/nuwave/lighthouse/pull/525/files
Or alternatively use a group directive:
https://lighthouse-php.com/3.0/api-reference/directives.html#group

Filter by Email in prisma GraphQL

I'm trying to filter a user by email, I am using prisma and doing tests with the "playground", is there any way to filter the user's data by email?
My schema:
interface Model {
id: ID! #id
createdAt: DateTime! #createdAt
updatedAt: DateTime! #updatedAt
}
type User implements Model {
id: ID! #id
createdAt: DateTime! #createdAt
updatedAt: DateTime! #updatedAt
username: String
name: String!
surname: String!
email: String!
token_usuario: String! #unique
}
My attempts:
Using prisma 2.0, the db client exposes something like this:
ctx.prisma.user.findMany({
where: {
email: { contains: "something" }
}
});
And I expect the query would follow suit. So try passing in the where field for filtering. Would need to see the full schema to say for sure.

How to access model hasMany Relation with where condition in lighthouse graphql?

I have a model Media with a type column. I want to get files by type with a dynamic where condition.
Model
public function images () {
return $this->hasMany(Media::class, 'recipe_id');
}
public function collection($type) {
return $this->images()->where('collection', '=', $type);
}
Schema.graphql
type User {
id: ID!
title: String!
images: [Media] #hasMany
}
type Media {
id: ID!
collection: String!
name: String!
file: String!
mime_type: String!
user: User #belongsTo
}
The media relationship will give all the images related to this user but I want to access the collection method inside the model.
That should make a trick, and you do not need collection in your Model
Schema.graphql
type User {
id: ID!
title: String!
images (type: String #eq): [Media] #hasMany
}
type Media {
id: ID!
type: String
name: String!
file: String!
mime_type: String!
user: User #belongsTo
}
Or you can do that simply by custom Query but solutions above seams to be more elegant

Assigning a Type to a field in GraphQL Mutation

In the example below I am trying to create a Creature in my database each creature has many attributes so I created an CreatureAttribute Type with many different required String and Int fields. How do I attach that Type to the Creature Type Mutation?
mutation{
createCreature(data: {
creature_name: "Drake"
creature_type: "Dragon"
creature_size: "Huge"
description: "description Text..."
habitat: "habitat text..."
combat: "combat text..."
additional_info: "additional info text..."
attributes: ********this is where I would like to bring in my CreatureAttributes Type********
)
{
creature_name
creature_type
description
habitat
combat
additional_info
attributes
}
}
Thank you for your answers in advance :)
As per my understanding you want to create a relation "Creature has may attributes". So attribute should accept array as shown in following sample.
type Creature {
id: ID! #unique
creature_name: String!
creature_type: String!
creature_size: String!
description: String!
habitat: String!
combat: String!
additional_info: String!
attributes: [CreatureAttributes]!
}
type CreatureAttributes {
strength: Int!
health: Int!
stamina: Int!
mana: Int!
reaction: Int!
...}
Ref - https://graphql.org/learn/schema/#object-types-and-fields

Resources