Using Custom with a nullable variable

I have the schema:

type Project {
  id: ID
  name: String!
  url: String
  icons: Icons @custom(http: {
    url: "http://favicongrabber.com/api/grab/$url"
    method: GET
  })
  description: String
}

type Icons @remote {
  domain: String
  icons: [Icon]
  error: String
}

type Icon @remote {
  src: String!
  type: String
  sizes: String
}

However this is not able to be deployed due to the following error:

resolving updateGQLSchema failed because input:29: Type Project; Field icons; url path inside @custom directive uses a field url that can be null.

I can’t make the url field a mandatory field because not every project will have a url, but I do want to use the custom with that field. Any resolution to this besides making url a mandatory field?

An idea would be to use a default value for the URL, with a URL for blank favicons. Then your Project can look like this:

type Project{
   id: ID!
   name: String!
   url: String!
   icons: ...
}

Right now Dgraph doesn’t support default values yet, so that will have to be programmed in your frontend. But I’ve been thinking about this for some time now, and this seems to be a good thing to have.

EDIT: I just realized that @lambda had landed in the latest versions. So you can just use @lambda to do your default values for you. Docs here:

https://dgraph.io/docs/graphql/lambda/overview/

1 Like