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

Support for new hasura naming convention (graphql-default) #130

Open
spion opened this issue Sep 24, 2022 · 3 comments
Open

Support for new hasura naming convention (graphql-default) #130

spion opened this issue Sep 24, 2022 · 3 comments

Comments

@spion
Copy link

spion commented Sep 24, 2022

I tried to do this by modifying the operation template

function decapitalize(s: string) {
  if (s.length < 1) return s;
  return s.charAt(0).toLowerCase() + s.substring(1);
}
const operationTemplate = {
  GET_LIST: (resource: { name: string }) => `${decapitalize(resource.name)}`,
  GET_ONE: (resource: { name: string }) => `${decapitalize(resource.name)}`,
  GET_MANY: (resource: { name: string }) => `${decapitalize(resource.name)}`,
  GET_MANY_REFERENCE: (resource: { name: string }) => {
    console.log("Getting reference for", resource);
    return `${decapitalize(resource.name)}`;
  },
  CREATE: (resource: { name: string }) => `insert${resource.name}`,
  UPDATE: (resource: { name: string }) => `update${resource.name}`,
  UPDATE_MANY: (resource: { name: string }) => `update${resource.name}`,
  DELETE: (resource: { name: string }) => `delete${resource.name}`,
  DELETE_MANY: (resource: { name: string }) => `delete${resource.name}`,
};

However this is insufficient as relationships such as e.g. otherEntity cannot be found

Unknown resource otherEntities. Make sure it has been declared on your server side schema. Known resources are OtherEntity, ...
@arjunyel
Copy link
Contributor

Do you by chance have any repo I can test out?

@cassus
Copy link

cassus commented Dec 24, 2022

I found a way to make it work:

function lowerFirst(s: string) {
  if (s.length < 1) return s
  return s.charAt(0).toLowerCase() + s.substring(1)
}
type Resource = { name: string }
const introspectionOptions = {
  include: ["Surveys", "SurveyAccess", "Users"],
  operationNames: {
    [GET_LIST]: (resource: Resource) => `${lowerFirst(resource.name)}`,
    [GET_ONE]: (resource: Resource) => `${lowerFirst(resource.name)}`,
    [GET_MANY]: (resource: Resource) => `${lowerFirst(resource.name)}`,
    [GET_MANY_REFERENCE]: (resource: Resource) =>
      `${lowerFirst(resource.name)}`,
    [CREATE]: (resource: Resource) => `insert${resource.name}`,
    [UPDATE]: (resource: Resource) => `update${resource.name}`,
    [UPDATE_MANY]: (resource: Resource) => `update${resource.name}`,
    [DELETE]: (resource: Resource) => `delete${resource.name}`,
    [DELETE_MANY]: (resource: Resource) => `delete${resource.name}`,
  },
}

useEffect(() => {
    if (!client) return

    const buildDataProvider = async () => {
      const _dataProvider = await buildHasuraProvider(
        {
          client,
          introspection: introspectionOptions,
        },
        {
          aggregateFieldName(resourceName) {
            return `${lowerFirst(resourceName)}Aggregate`
          },
        },
      )
      setDataProvider(() => _dataProvider)
    }
    void buildDataProvider()
  }, [client])

@gregorojstersek
Copy link

Thanks @cassus that works great!

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

4 participants