I am developing the GraphQL Node tutorial, and am up to step 7.
https://www.howtographql.com/graphql-js/7-subscriptions/
I am getting a number of syntax errors from this code in my datamodel.prisma file:
directive #id on FIELD_DEFINITION
directive #unique on FIELD_DEFINITION
directive #createdAt on FIELD_DEFINITION
scalar DateTime
type Link {
id: ID! #id
createdAt: DateTime! #createdAt
description: String!
url: String!
postedBy: User
votes: [Vote!]!
}
type User {
id: ID! #id
name: String!
email: String! #unique
password: String!
links: [Link!]!
votes: [Vote!]!
}
type Vote {
id: ID! #id
link: Link!
user: User!
}
But am still getting 'User' type [#6:1] tried to redefine existing 'User' type [#15:5] and 'Link' type [#24:1] tried to redefine existing 'Link' type [#6:5].
I am also not sure if I am declaring directives or scalars correctly as this is missing from the official tutorial.
Can anyone give any advice on how to sort these issues?
Schema.graphql:
type Query {
info: String!
feed(filter: String, skip: Int, first: Int, orderBy: LinkOrderByInput): Feed!
}
type Feed {
links: [Link!]!
count: Int!
}
type AuthPayload {
token: String
user: User
}
type User {
id: ID!
name: String!
email: String!
links: [Link!]!
}
type Vote {
id: ID!
link: Link!
user: User!
}
type Link {
id: ID!
description: String!
url: String!
postedBy: User
votes: [Vote!]!
}
type Subscription {
newLink: Link
newVote: Vote
}
type Mutation {
post(url: String!, description: String!): Link!
signup(email: String!, password: String!, name: String!): AuthPayload
login(email: String!, password: String!): AuthPayload
vote(linkId: ID!): Vote
}
enum LinkOrderByInput {
description_ASC
description_DESC
url_ASC
url_DESC
createdAt_ASC
createdAt_DESC
}
I had the same error this morning and I have a feeling it has something to do with caching. It went away when I renamed the variable. In your case, change all 'Link' definition/references to 'LinkTwo' and see if the error goes away. Same with 'User'... change it to 'UserTwo'. If it does, perhaps you can rename them back afterwards.
I haven't used Prisma before and only glanced at the tutorial but it looks like you're defining two User types; you have one in datamodel.prisma and Schema.graphql (I couldn't find two definitions of User in the tutorial). If they are read into the same instance, that'll be the reason why graphql thinks you are trying to redefine the User type. Remove one or, if it's applicable, change the second to extend type User should solve the syntax error.
Related
I have already created a hasRole directive. I only want to include certain input parameters if the user has the admin role:
const typeDefs = gql`
type Article {
id: ID!
title: String!
content: String!
sponsor: String
}
input CreateArticleInput {
title: String!
content: String!
sponsor: String // include if #hasRole(role: "admin")
type Mutation {
createArticle(input: CreateArticleInput!): Article!
publishArticle(articleId: ID!): Boolean! #hasRole(role: "editor")
}
`
The goal is to provide the sponsor input parameter optionally if the user has the role "admin", otherwise, do not let the user include a "sponsor". I wonder if the native #include or #skip directives can be used in tandem with my custom #hasRole directive to achieve this? ..just a thought.
Is something like this possible? I am not even sure if this is allowed in the input type.
I'm creating an API with AWS Amplify and AppSync GraphQL. I Work on a Tinder like App.
I would like to search the same value in two fields for a Query. I've tried a lot of solutions, but none seems to work.
I have a User #Model which is connected to many Match via #connection
type User #model
#auth(rules: [{allow: public}])
{
id: ID!
email: String!
password: String
age: String!
name: String!
description: String
pickupLine: String
orientation: Orientation!
gender: Gender!
animal: Animal!
profilePicture: String!
pictures: [String]!
location: Location
numberOfSparksAvailable: Int!
matchs: [Match]
#connection(keyName: "byUser", fields: ["id"])
sparks: [Spark]
#connection(keyName: "byUser", fields: ["id"])
}
And I have a Match #Model which have a senderId field and a receiverId field
type Match #model
#auth(rules: [{allow: public}])
#key(name: "byUser", fields: ["receiverId", "senderId"], queryField: "matchByUser")
#key(name: "bySender", fields: ["senderId"], queryField: "sparkBySender")
#key(name: "byReceiver", fields: ["receiverId"], queryField: "sparkByReceiver")
{
id: ID!
status: MatchStatus!
matchType: MatchType
sender: User! #connection(fields: ["senderId"])
senderId: ID!
receiver: User! #connection(fields: ["receiverId"])
receiverId: ID!
answerFrom: Answer!
answerTo: Answer
chatRoomId: ID!
chatRoom: ChatRoom!
#connection(fields: ["chatRoomId"])
deletedAt: AWSDate
}
And and I don't find any way to make bySender #key look for both senderId and receiverId. I want to be able to have all the Matches for a User whether he's the receiver or the sender.
But when I make a Query to ListUsers I only get the Matches where the User is a receiver.
I've also tried to make multiple #connection but it is not allowed by Graphql-transformer.
I also tried to make an usersId: [ID!]! like that
type Match #model {
...
usersId: [ID!]!
users: [User!]!
#connection(fields: ["usersId"])
...
}
But It's also not allowed by graphql-transformer..
Has anyone a solution to get all the matches for a User?
Thanks a lot !
Hi I'm making a backend server with GraphQL, Apollo client & Prisma. I'm trying to write a query where I get organization data back. The user who sends the query should get its organization data back based on their id. When running the query in playground I get this error.
error:
"message": "Variable '$where' expected value of type 'OrganizationWhereUniqueInput!' but got: {\"employees\":{\"id\":\"ckas83z13t9qk0992pucglc4k\"}}. Reason: 'employees' Field 'employees' is not defined in the input type 'OrganizationWhereUniqueInput'. (line 1, column 8):\nquery ($where: OrganizationWhereUniqueInput!) {\n ^",
I don't see what I did wrong. I'm still pretty new to it all. I tried to write the function in Query.js in different ways but no luck. Also, I still find the error messages you get in playground very confusing
schema:
type Query {
getOrganization: Organization!
}
type Organization {
id: ID!
name: String!
country: String!
street: String!
zipCode: Int!
houseNumber: Int!
addings: String
employees: [User!]
}
type User {
id: ID!
firstname:String!
lastname:String!
email: String!
services: [Service!]
organization: Organization!
}
query.js
function getOrganization(parent, args, context, info){
const userId = getUserId(context)
return context.prisma.organization({employees:{id:userId}})
}
// also tried this
/*
function getOrganization(parent, args, context, info){
const userId = getUserId(context)
return context.prisma.organization({where: {employees:{id:userId}}})
}*/
User.js
function services (parent, args, context){
return context.prisma.user({id: parent.id}).services()
}
function organization (parent, args, context){
return context.prisma.user({id: parent.id}).organization()
}
module.exports={
services,
organization
}
Organization.js
function employees(parent, args, context){
return context.prisma.organization({id: parent.id}).employees()
}
module.exports={
employees
}
Could anyone help me see what went wrong?
query in playground:
query{
getOrganization{
name
id
}}
HTTP HEADER:
{
"Authorization": "Bearer {contains user token }"
}
Just use OrganizationWhereInput instead of OrganizationWhereUniqueInput. It will return a list of organisations instead of a single result (might return an empty array), yet it should allow you to search for an organisation using an employee id.
I'm creating a shopping cart in my frontend, and I want to pass all the items in the shopping cart as an array to my mutation addLicense on my backend, so I don't have to make multiple API calls. Is this possible? If so, how, or what are my options?
I've tried to add square brackets around the arguments like this
extend type Mutation {
addLicense([
licenseType: String!
licenseAmount: String!
isActive: Boolean
expirationDate: Date!
]): License!
}
I've also tried to set the object type as an argument
extend type Mutation {
addLicense(
license: [License]
): License!
First attempt throws this error
/app/node_modules/graphql/language/parser.js:1463
throw (0, _error.syntaxError)(lexer.source, token.start, "Expected ".concat(kind, ", found ").concat((0, _lexer.getTokenDesc)(token)));
^
GraphQLError: Syntax Error: Expected Name, found [
at syntaxError
(/app/node_modules/graphql/error/syntaxError.js:24:10)
The other attempt throws this error
Error: The type of Mutation.addLicense(license:) must be Input Type but got: [License].
your mutation:
type Mutation {
"""
Add multiple items to basket
"""
addLicenses(licenses: [License!]!): LicenseMutationBatchResult
}
your interface input:
input License {
licenseType: String!
licenseAmount: String!
isActive: Boolean
expirationDate: Date!
}
whatever you give back:
type LicenseMutationBatchResult {
...whatever you give back here...
}
I got some advices over at the GraphQL Slack. I solved it by adding a new Input Type.
input LicenseInput {
id: ID
licenseType: String!
licenseAmount: String!
isActive: Boolean
expirationDate: Date
course: String!
organizationID: String!
price: Int!
}
I was then able to add my mutation like so
extend type Mutation {
addLicense(
license: [LicenseInput] <-
): License!
}
I've been reading and trying alot of Stuff, but only using cURL as tools. Are there other ways run tests against an GraphQL endpoint?
So my problem is an mutation query with serveral variables. Here is part of my Shema:
type mutation {
oneComponentTypeCollected(userID: String!, password: String!, pageID: Int!,
Components: ComponentsInput!): Page
}
type Components {
ComponentType: ComponentType!
required: Int!
collected: Int!
}
type ComponentsInput {
id: String!
collected: Int!
}
type Page {
id: Int!
}
How do i call query the oneComponentCollected mutation?
What is expected is to set the collected value to 1 of the Component "inkpad_blue"
I tried the Following:
Variable syntax:
{"query":"mutation($input:ComponentsInput!) {oneComponentTypeCollected(userID: \"asd\", password: \"asd\", pageID: 1, input:$input){id}}","variables":{"input":{"id":"Inkpad_blue","collected":1}}}
as pure String:
mutation($input:ComponentsInput!) {oneComponentTypeCollected(userID: "asd", password: "asd", pageID: 1, input:$input){id}}
Both result in 500 HTTP errors.
I think your mutation should go like this
mutation{oneComponentTypeCollected(userID: "asd", password: "asd", pageID: 1, Components:{"id": "Inkpad_blue","collected": 1}){id}}
Try this in your graphiql