Use ref as an index arg in graphql schema

Hi,

I’m trying to use the ref as an arg, is it possible?
Here’s the schema

type Project {
    user: String
    name: String!
}

type Query {
    projectsByUser(user: String!): [Project!]!
    projectsByUserAndRef(ref: String!, user: String!): Project
}

When I import it, I get this error

I am able to create it manually though

How to use the ref in schema.gql, is it possible?

Thank you

A Ref is an object in FQL, not a string. The ID is a string. GraphQL abstracts this away.

The GraphQL “way” to do this is to get the item by its ID, i.e with findProdectById query, and then select the field you want in the result. No additional index is required.

To use the index you show, you would likely need to create a custom resolver to convert the id provided to a ref.

1 Like

Thank you for your reply
I tried using the id, but it was not accepted, here’s my schema

type Project {
    user: String
    name: String!
}

type Query {
    projectsByUser( user: String! ): [Project!]!
    findProjectByIDAndUser( id: ID!, user: String! ): Project
}

type Mutation {
    deleteUserProject( id: ID!, user: String!): ID! @resolver(name: "delete_user_project")
}

I got this error

I noticed that the id is used by faunadb code (as shown in the rectangle) so I’m confused why it wouldn’t accept it when I introduce it
Maybe the the type Project should be altered in some way? That’s the only thing I suspect

If you have any idea, I would really appreciate it!
Thank you

I will use this approach in the custom resolver, I worked fine

Let ({
  ref: Ref(Collection("Project"), Var("id"))
}, 
If( 
  Equals(Select(["data", "user"], Get(Var("ref"))), Var("user")), 
  Delete(Var("ref")), 
  Abort("No Permissions"))
)

You misunderstand what or have missed what I said.

To clarify, each Documents id is unique. That is, it’s Ref is unique and graphQL will send you the id. Searching by id AND anything else is redundant – do not try to search by ID + anything else. You can use just findProjectByID, and then query for the user.

{
  findProjectByID(id: "1234") {
    _id
    _ts
    user
    name
  }
}

If you want to search by id OR the user string, then you will need to create a UDF customer resolver.

beyond that you can start with the docs to try to develop Indexes that will work with GraphQL and if necessary custom resolvers.

1 Like

Thank you

I will follow the simple approach of reading the document then checking the user