GraphQL--any way to get Metadata from schema?

Is there any way to get the metadata that describes the data type with field names in a given object schema? I don’t mind even a UDF solution that can be called from GraphQL.

It sounds like you need an introspection query. I haven’t tried with Fauna yet but something like GQL codegen might work for you.

Thanks. I will check it out.

I’m not sure if this works for you, but the current GQL meta data is stored inside the Collection ref, you can get directly with:
Get(Collection("YourCollection"))

eg:

Select(['data','gql','meta'],Get(Collection("Stock")))

{
  name: "Stock",
  fields: [
    {
      name: "ticker",
      type: {
        Named: "String"
      }
    },
    {
      name: "name",
      type: {
        NotNull: {
          Named: "String"
        }
      }
    },
    {
      name: "market",
      type: {
        NotNull: {
          Named: "String"
        }
      }
    },
    {
      name: "lastPrice",
      type: {
        NotNull: {
          Named: "Float"
        }
      }
    }
  ],
  directives: [
    {
      name: "collection",
      args: {
        name: "Stock"
      }
    }
  ]
}

ps: bear in mind that this could change in the future

That’s exactly what I want. But it’s FQL. Is there a GraphQL way to get it? I guess it might be possible through a UDF but am not sure.

Thanks.

You have to create an UDF + Type + Query, eg:

CreateFunction({
  name: 'fetch_qql_meta',
  body: 
    Query(
      Lambda(
        ['ref_name'],
        Map(
          Select(["data", "gql", "meta", "fields"], Get(Collection(Var('ref_name')))),
          r => ToObject({ 
              'name': Select('name', r),  
              'type': If(Contains(['type', 'NotNull', 'Named'], r),
                 Select(['type', 'NotNull', 'Named'], r), 
                 Select(['type', 'Named'], r)), 
              'nullable': Not(Contains(['type', 'NotNull'], r))
          })
        )
      )
    )
})

the result will be something like:

Call('fetch_qql_meta', ['Stock'])

[
  {
    name: "ticker",
    type: "String",
    nullable: true
  },
  {
    name: "name",
    type: "String",
    nullable: false
  },
  {
    name: "market",
    type: "String",
    nullable: false
  },
  {
    name: "lastPrice",
    type: "Float",
    nullable: false
  }
]

then, the graphql DDL:

type GQLMeta @embedded {
	name: String!
	type: String!	
	nullable: Boolean!
}

type Query {
	fetchGQLMeta(ref_name: String): [GQLMeta!] @resolver(name: "fetch_qql_meta")
}

graphql command from Playground:

# Write your query or mutation here
query GetStockMata {
  fetchGQLMeta(ref_name: "Stock") {
    name
    nullable
    type
  }
}

returns:

{
  "data": {
    "fetchGQLMeta": [
      {
        "name": "ticker",
        "nullable": true,
        "type": "String"
      },
      {
        "name": "name",
        "nullable": false,
        "type": "String"
      },
      {
        "name": "market",
        "nullable": false,
        "type": "String"
      },
      {
        "name": "lastPrice",
        "nullable": false,
        "type": "Float"
      }
    ]
  }
}

You can get more details here https://docs.fauna.com/fauna/current/api/graphql/functions

1 Like

Thanks. I appreciate it. It will take me some time to understand and try it out.

1 Like