Declare variable in lighthouse graphql files - laravel

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

Related

Using graphql, I need to crate a mutation which includes an empty array

Below, I'm attempting to create a type Customer which will include and array of type Box.
In my addCustomer mutation, I need to create an empty boxes array which will later have Boxes pushed into to it. I'm getting this error:
Error: The type of Mutation.addCustomer(boxes:) must be Input Type but got: [Box].
Any ideas?
const { gql } = require('apollo-server-express');
const typeDefs = gql`
type Customer {
_id: ID
firstName: String
lastName: String
email: String
password: String
address: String
city: String
state: String
zip: String
phone: String
boxes: [Box]
}
type Auth {
token: ID!
customer: Customer
}
type Box {
_id: ID!
boxSize: String!
sendToCustomer: Boolean!
getFromCustomer: Boolean!
}
type Mutation {
addCustomer(
firstName: String!,
lastName: String!,
email: String!,
password: String!,
address: String!,
city: String!,
state: String!,
zip: String!,
phone: String!
boxes: [Box]
): Auth
login(email: String!, password: String!): Auth
createBox(
boxSize: String!,
sendToCustomer: Boolean!,
getFromCustomer: Boolean!
): Box
addBoxToCustomer(
customerId: ID!,
boxSize: String!,
sendToCustomer: Boolean!,
getFromCustomer: Boolean!
): Customer
}
In GraphQL Input Object Types and Object Types are not compatible. This means that you can't use an output object type as an input object type, even if they have the same structure.
For customer you have circumvented the problem by defining every field as an argument to the mutation. First, I would create an Box input type. I like to call these input types drafts and postfix them with Draft but many people in the community postfix with Input.
input BoxDraft {
boxSize: String!
sendToCustomer: Boolean!
getFromCustomer: Boolean!
}
Notice how we can leave out the _id field, as I assume it is automatically generated by your server implementation. Then you can reference this type in your mutation.
type Mutation {
addCustomer(
firstName: String!,
lastName: String!,
email: String!,
password: String!,
address: String!,
city: String!,
state: String!,
zip: String!,
phone: String!
boxes: [BoxDraft]
): Auth
# ...
}
I would go even one step further and also define a draft type for Customer:
input CustomerDraft {
firstName: String!
lastName: String!
email: String!
password: String!
address: String!
city: String!
state: String!
zip: String!
phone: String!
boxes: [BoxDraft]
}
type Mutation {
addCustomer(draft: CustomerDraft!): Auth
}

GraphQL - Model(s) belong to specific user field

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.

No directive found for `bcrypt`

I'm following official Lighthouse documentation on how to setup GraphQl into my project.
Unfortunately, I'm stacked with the following error:
No directive found for `bcrypt`
Following schema I have created so far:
type Query {
users: [User!]! #paginate(defaultCount: 10)
user(id: ID #eq): User #find
}
type User {
id: ID!
name: String!
email: String!
created_at: DateTime!
updated_at: DateTime!
}
type Mutation {
createUser(
name: String!,
email: String! #rules(apply: ["email", "unique:users"])
password: String! #bcrypt
): User #create
}
The query I'm trying to execute is following:
mutation {
createUser(
name:"John Doe"
email:"john#test.com"
password: "somesupersecret"
) {
id
email
}
}
Okay dug up - there is no more #brypt directory in \vendor\nuwave\lighthouse\src\Schema\Directives\ instead, there is #hash directive which is working like a charm and uses driver you specify in config/hashing.php configuration file

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.

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