Filter by Email in prisma GraphQL - 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.

Related

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.

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.

GraphQL Prisma - use orderBy on nested relationships

I am fairly new to GraphQL Prisma combo, I cannot seem to order nested relationships without error because the only available orderBy fields are regarding the ID.
My intended goal in the playground would be to create a query that looks similar to the following:
getPosts{
connections(orderBy: "text_asc"){
id
text
}
}
Resolver
getPosts(parent, args, ctx, info){
return ctx.db.query.posts({}, info)
}
Data Model
type Post {
id: ID! #id
isPublished: Boolean! #default(value: false)
title: String!
text: String!
connections: [Connection!]! #relation(name: "ClientConnect")
}
type Link {
id: ID! #id
text: String!
url: String!
}
type Connection {
id: ID! #unique #id
client: Link #relation(name: "LinkConnect")
}

Prisma A link table must not specify any additional scalar fields

I have a many to many relation between two tables:
type Post {
id: ID! #id
createdAt: DateTime! #createdAt
updatedAt: DateTime! #updatedAt
name: String!
users: [Profile!] #relation(link: TABLE, name: "ParticipatedUsers")
}
type Profile {
id: ID! #id
createdAt: DateTime! #createdAt
updatedAt: DateTime! #updatedAt
name: String!
participatingInPosts: [Post!] #relation(name: "ParticipatedUsers")
}
type ParticipatedUsers #relationTable {
post: Post
profile: Profile
requiredPostsCount: Int
publishedPostsCount: Int
}
and when trying to run the command prisma deploy i got the following errors:
Errors:
ParticipatedUsers
✖ A link table must not specify any additional scalar fields.
✖ A link table must not specify any additional scalar fields.
✖ A link table must not specify any additional scalar fields.
how could i create a relation table and add a new fields to that table?
Thanks,
In Prisma you can do this by adding a type with scalar fields and two relations to the types you want to connect, e.g.:
type Post {
id: ID! #id
createdAt: DateTime! #createdAt
updatedAt: DateTime! #updatedAt
name: String!
users: [ParticipatedUser!] #relation(link: TABLE, name: "ParticipatedUserPosts")
}
type Profile {
id: ID! #id
createdAt: DateTime! #createdAt
updatedAt: DateTime! #updatedAt
name: String!
participatingInPosts: [ParticipatedUser!] #relation(link: TABLE, name: "ParticipatedUserProfiles")
}
type ParticipatedUser {
post: Post! #relation(name: "ParticipatedUserPosts")
profile: Profile! #relation(name: "ParticipatedUserProfiles")
requiredPostsCount: Int
publishedPostsCount: Int
}

Error: Valid values for the strategy argument of `#scalarList` are: RELATION

Program pops up this -> (Valid values for the strategy argument of #scalarList are: RELATION.) after run prisma deploy. Any one knows why ?
type User {
id: ID! #id
name: String!
email: String! #unique
password: String!
age: Int
img: String
location: Location
hostedEvents: [Event]! #relation(name: "HostedEvents", onDelete: CASCADE)
joinedEvents: [Event]! #relation(name: "EventMembers", onDelete: CASCADE)
pushNotificationTokens: [PushNotificationTokens]!
createdAt: DateTime! #createdAt
updatedAt: DateTime! #updatedAt
}
type Event {
id: ID! #id
owner: User! #relation(name: "HostedEvents")
name: String!
imgs: [String]!
description: String
start: DateTime!
end: DateTime!
categories: [Category]!
members: [User]! #relation(name: "EventMembers")
chatRoom: GroupChatRoom!
pendingRequests: [PendingRequest]!
locations: [Location]!
comments: [Comment]!
createdAt: DateTime! #createdAt
updatedAt: DateTime! #updatedAt
}
According with docs, when we need to create Fields as Array or List, the directive #scalarlist directive is required, in your case the correct model definition should be with the table/column imgs
type Event {
id: ID! #id
owner: User! #relation(name: "HostedEvents")
name: String!
imgs: [String!]! #scalarList(strategy: RELATION)
description: String
start: DateTime!
end: DateTime!
categories: [Category]!
members: [User]! #relation(name: "EventMembers")
chatRoom: GroupChatRoom!
pendingRequests: [PendingRequest]!
locations: [Location]!
comments: [Comment]!
createdAt: DateTime! #createdAt
updatedAt: DateTime! #updatedAt
}
Docs link -> https://www.prisma.io/docs/datamodel-and-migrations/datamodel-MYSQL-knul/##scalarlist

Resources