Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GraphQL directives not applied #16

Closed
aenawi opened this issue Aug 12, 2021 · 5 comments
Closed

GraphQL directives not applied #16

aenawi opened this issue Aug 12, 2021 · 5 comments

Comments

@aenawi
Copy link

aenawi commented Aug 12, 2021

Hi

I'm trying to apply fastgql directive (@tableName) in my graphql schema file like so:

type UserProfile @generateFilterInput(name: "UserProfileFilterInput") @tableName(name: "myapp.user_profile") {
    userID:  ID! @sqlRelation(relationType: ONE_TO_ONE, baseTable: "myapp_private.user_account", refTable: "janaez.user_profile", fields: ["id"], references: ["user_id"])
    firstname: String
    lastname: String
    displayname: String!
    createdAt: Time
    updatedAt: Time
    nationality: Country @sqlRelation(relationType: ONE_TO_ONE, baseTable: "myapp.country", refTable: "myapp.user_profile", fields: ["id"], references: ["nationality_id"])
}

type UserProfilePayload {
    userID:  ID!    # userAccount.id
    firstname: String
    lastname: String
    displayname: String
    createdAt: Time
    updatedAt: Time
    nationality: Country
}

input UserProfileQueryInput {
    firstname: String
    lastname: String
    displayname: String
    nationalityID: Int
}

...and declared my GraphQL Query like so:

type Query {
#   UserProfile
    users(input: UserProfileQueryInput): [UserProfilePayload!]!
}

The problem:
When I run the GraphQL playground and try my query statement like this:

query GetAllUsers {
  users{
    userID
    firstname
    lastname
    displayname
    createdAt
    updatedAt
    nationality {
      id
    }
  }
}

I receive this error:

{
  "errors": [
    {
      "message": "ERROR: relation \"users\" does not exist (SQLSTATE 42P01)",
      "path": [
        "users"
      ]
    }
  ],
  "data": null
}

It looks like it's still using the Query type name and not the actual TableName as defined in the schema using @tableName directive!

Any ides please?!

@roneli
Copy link
Owner

roneli commented Aug 14, 2021

Hi @aenawi, thanks for trying fastgql, is super early lib as you can see it doesn't have tests.

I will take a look why the directive didn't work correctly and get back to you.

@roneli
Copy link
Owner

roneli commented Aug 14, 2021

Hi, I found out what was causing the problem, the result type of users needs to be the same as the type its querying i.e [UserProfiles].
Moreover, I recommend as defined in docs to use the @generate directive as it adds limit,offset, ordering and automatic filtering.

Note: that this library is really in the start and I still need to add support for adding more complex filter support.

GraphQL schema I used to check your problem.


scalar Time

type Country {
  id: String
  name: String
}

type UserProfile @generateFilterInput(name: "UserProfileFilterInput") @tableName(name: "myapp.user_profile") {
  userID:  ID! @sqlRelation(relationType: ONE_TO_ONE, baseTable: "myapp_private.user_account", refTable: "janaez.user_profile", fields: ["id"], references: ["user_id"])
  firstname: String
  lastname: String
  displayname: String!
  createdAt: Time
  updatedAt: Time
  nationality: Country @sqlRelation(relationType: ONE_TO_ONE, baseTable: "myapp.country", refTable: "myapp.user_profile", fields: ["id"], references: ["nationality_id"])
}

type Query @generate {
  #   UserProfile
  users: [UserProfile!]!
}

Hope this helps, if you like this project, feel free to contribute, ill be more than happy to merge features/get feature requests you might want

@aenawi
Copy link
Author

aenawi commented Aug 19, 2021

Thanks, yes I've noticed no tests available, just got curious to give it a try ;) I like when smart developers think in different ways.

Anyways, so far these are the results from what I understood:

First, after making the changes you mentioned above, yes the query got corrected, but still I've received this error from GraphQL:


{
  "errors": [
    {
      "message": "ERROR: relation \"myapp.user_profile\" does not exist (SQLSTATE 42P01)",
      "path": [
        "users"
      ]
    }
  ],
  "data": null
}

and when I debugged the Users resolver in (shema.fastgql.go) file, I've noticed that sql.NewBuilder created the following query which looks a bit confusing and it didn't accept "myapp.user_profile" (with quotes) in the sql statement! To test it I've copied the statement and ran it under PgAdmin console and "myapp.user_profile" (with quotes) was highlighted as an error. I removed the quotes and it did accept it but couldn't continue because of the rest LEFT JOIN LATERAL (FROM "userID") - that which I don't understand at this stage!


SELECT
	"duqtxi"."userID" AS "userID",
	"zpehps"."firstname" AS "firstname",
	"zpehps"."lastname" AS "lastname",
	"zpehps"."displayname" AS "displayname",
	"zpehps"."createdAt" AS "createdAt",
	"zpehps"."updatedAt" AS "updatedAt",
	"zyfjlr"."nationality" AS "nationality"
FROM "myapp.user_profile" AS "zpehps"
LEFT JOIN LATERAL (
	SELECT jsonb_build_object() AS "userID"
	FROM "userID" AS "duqtxi"
	WHERE zpehps.id = duqtxi.user_id
) AS "duqtxi" ON true
LEFT JOIN LATERAL (
	SELECT jsonb_build_object('id', "zyfjlr"."id", 'name', "zyfjlr"."name") AS "nationality" FROM "nationality" AS "zyfjlr"
	WHERE zpehps.nationality_id = zyfjlr.id
) AS "zyfjlr" ON true
LIMIT $1

another thing I've noticed from last LEFT JOIN LATERAL, why the name of the table in the FROM showing (nationality) and not (country) since Country was the type defined in the GraphQL schema for UserProfile!

Any ways, I stopped at the confusing part where the Executor.Query is not able to execute the statement built using sql.NewBuilde, b'cos of all those quotes and aliases! Do I need to reconfigure my PostgrSQL DB or something?!

I think I should wait for a while until you create some more examples, or add unit tests.

Thank you for all your efforts

@roneli
Copy link
Owner

roneli commented Sep 11, 2021

Hi sorry for taking so long to reply, I added a argument to the @tableName directive, this should solve this error "message": "ERROR: relation \"myapp.user_profile\" does not exist (SQLSTATE 42P01)", .

Started adding tests as well so hopefully ill get around to stabilizing the code some more and test more usecases.

@roneli
Copy link
Owner

roneli commented Sep 11, 2021

Fixed in #18 the LEFT JOIN LATERAL so the FROM should be based on the type rather than the selection. (it will use the type name if there is not @tablename directive on that type)

@roneli roneli closed this as completed Dec 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants