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

Cell Query Operation name not camelCase-d if use underscore in generation #883

Closed
dthyresson opened this issue Jul 23, 2020 · 5 comments · Fixed by #888
Closed

Cell Query Operation name not camelCase-d if use underscore in generation #883

dthyresson opened this issue Jul 23, 2020 · 5 comments · Fixed by #888
Labels
bug/confirmed We have confirmed this is a bug topic/generators-&-scaffolds

Comments

@dthyresson
Copy link
Contributor

dthyresson commented Jul 23, 2020

As of v0.14.0, cell queries now get an operation name to work with Storybook:

See: #867

Example:

export const QUERY = gql`
- query {
+ query TestQuery {
    userProfile {
     id
   }
  }
`

I created a cell via the generator:

yarn rw g cell my_sites

and

✔ Generating cell files...
    ✔ Writing 
`./web/src/components/MySitesCell/MySitesCell.mock.j
s`...
    ✔ Writing 
`./web/src/components/MySitesCell/MySitesCell.storie
s.js`...
    ✔ Writing 
`./web/src/components/MySitesCell/MySitesCell.test.j
s`...
    ✔ Writing `./web/src/components/MySitesCell/MySi
tesCell.js`...
✨  Done in 1.57s.

the files, used the MySite upper camelCasing.

However, my query operation is

export const QUERY = gql`
  query my_sitesQuery {
    mySites {
      id
    }
  }
`

When I generated SitesCell it is

export const QUERY = gql`
  query sitesQuery {
    sites {
      id
    }
  }
`

I expected either

query MySitesQuery {

or

``query mySitesQuery {`

but not

``query my_sitesQuery {`

That said:

yarn rw g cell MySites
yarn rw g cell mySites

generates

export const QUERY = gql`
  query MySitesQuery {
    mySites {
      id
    }
  }
`

Is there a preferred case?

I noticed that in the GraphQL documentation the example is PascalCase:

query HeroNameAndFriends {
  hero {
    name
    friends {
      name
    }
  }
}
@peterp
Copy link
Contributor

peterp commented Jul 23, 2020

Oooh, great catch, PascalCase is what I wanted 👍

@peterp peterp added the bug/confirmed We have confirmed this is a bug label Jul 23, 2020
@peterp
Copy link
Contributor

peterp commented Jul 23, 2020

@dthyresson Any interest in creating a PR for this? 🍫

@dthyresson
Copy link
Contributor Author

@peterp Happy to!

@dthyresson
Copy link
Contributor Author

Oh, I see that:

import pascalcase from 'pascalcase'

is already used in

cli/src/commands/generate/helpers.js

So, thinking it's just something like:

const uniqueOperationName = (name, index = 1) => {
  let operationName =
    index <= 1
      ? `${pascalcase(name)}Query`
      : `${pascalcase(name)}Query_${index}`
  if (!getCellOperationNames().includes(operationName)) {
    return operationName
  }
  return uniqueOperationName(name, index + 1)
}

and then adding some more tests for single, multi (and change the expected case) and then an underscore and a dash scenarios along the line of:

console.log(pascalcase('foo_bar-baz')); //=> 'FooBarBaz'
console.log(pascalcase('#foo+bar*baz')); //=> 'FooBarBaz'
console.log(pascalcase('$foo~bar`baz')); //=> 'FooBarBaz'

@dthyresson
Copy link
Contributor Author

Right because, usually your component name will not be pascal cased:

  const outputComponentName =
    componentName || pascalcase(paramCase(name)) + suffix

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug/confirmed We have confirmed this is a bug topic/generators-&-scaffolds
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants