Fix duplicate fragment user issue

This is refactoring all our fragments and fixing the warning about an
existing name `user`. Apparently, fragments should have a unique name
globally.

I decided to call `userFragment`, `postFragment` the fragments for one
object and use different names to query for related objects.

I would be glad to learn a better way to handle this.
This commit is contained in:
roschaefer 2019-12-14 00:24:36 +01:00
parent b94a477bb1
commit 107409ecee
3 changed files with 146 additions and 61 deletions

View File

@ -1,6 +1,6 @@
import gql from 'graphql-tag' import gql from 'graphql-tag'
export const linkableUserFragment = lang => gql` export const userFragment = gql`
fragment user on User { fragment user on User {
id id
slug slug
@ -10,19 +10,8 @@ export const linkableUserFragment = lang => gql`
deleted deleted
} }
` `
export const userFragment = lang => gql` export const locationAndBadgesFragment = lang => gql`
fragment user on User { fragment locationAndBadges on User {
id
slug
name
avatar
disabled
deleted
shoutedCount
contributionsCount
commentedCount
followedByCount
followedByCurrentUser
location { location {
name: name${lang} name: name${lang}
} }
@ -33,15 +22,17 @@ export const userFragment = lang => gql`
} }
` `
export const postCountsFragment = gql` export const userCountsFragment = gql`
fragment postCounts on Post { fragment userCounts on User {
commentsCount
shoutedCount shoutedCount
shoutedByCurrentUser contributionsCount
emotionsCount commentedCount
followedByCount
followedByCurrentUser
} }
` `
export const postFragment = lang => gql`
export const postFragment = gql`
fragment post on Post { fragment post on Post {
id id
title title
@ -54,9 +45,22 @@ export const postFragment = lang => gql`
slug slug
image image
language language
author { pinnedAt
...user imageAspectRatio
} }
`
export const postCountsFragment = gql`
fragment postCounts on Post {
commentsCount
shoutedCount
shoutedByCurrentUser
emotionsCount
}
`
export const tagsCategoriesAndPinnedFragment = gql`
fragment tagsCategoriesAndPinned on Post {
tags { tags {
id id
} }
@ -71,11 +75,10 @@ export const postFragment = lang => gql`
name name
role role
} }
pinnedAt
imageAspectRatio
} }
` `
export const commentFragment = lang => gql`
export const commentFragment = gql`
fragment comment on Comment { fragment comment on Comment {
id id
createdAt createdAt
@ -84,8 +87,5 @@ export const commentFragment = lang => gql`
deleted deleted
content content
contentExcerpt contentExcerpt
author {
...user
}
} }
` `

View File

@ -1,20 +1,42 @@
import gql from 'graphql-tag' import gql from 'graphql-tag'
import { userFragment, postFragment, commentFragment, postCountsFragment } from './Fragments' import {
userFragment,
postFragment,
commentFragment,
postCountsFragment,
userCountsFragment,
locationAndBadgesFragment,
tagsCategoriesAndPinnedFragment,
} from './Fragments'
export default i18n => { export default i18n => {
const lang = i18n.locale().toUpperCase() const lang = i18n.locale().toUpperCase()
return gql` return gql`
${userFragment(lang)} ${userFragment}
${postFragment(lang)} ${userCountsFragment}
${locationAndBadgesFragment(lang)}
${postFragment}
${postCountsFragment} ${postCountsFragment}
${commentFragment(lang)} ${tagsCategoriesAndPinnedFragment}
${commentFragment}
query Post($id: ID!) { query Post($id: ID!) {
Post(id: $id) { Post(id: $id) {
...post ...post
...postCounts ...postCounts
...tagsCategoriesAndPinned
author {
...user
...userCounts
...locationAndBadges
}
comments(orderBy: createdAt_asc) { comments(orderBy: createdAt_asc) {
...comment ...comment
author {
...user
...userCounts
...locationAndBadges
}
} }
} }
} }
@ -24,14 +46,23 @@ export default i18n => {
export const filterPosts = i18n => { export const filterPosts = i18n => {
const lang = i18n.locale().toUpperCase() const lang = i18n.locale().toUpperCase()
return gql` return gql`
${userFragment(lang)} ${userFragment}
${postFragment(lang)} ${userCountsFragment}
${locationAndBadgesFragment(lang)}
${postFragment}
${postCountsFragment} ${postCountsFragment}
${tagsCategoriesAndPinnedFragment}
query Post($filter: _PostFilter, $first: Int, $offset: Int, $orderBy: [_PostOrdering]) { query Post($filter: _PostFilter, $first: Int, $offset: Int, $orderBy: [_PostOrdering]) {
Post(filter: $filter, first: $first, offset: $offset, orderBy: $orderBy) { Post(filter: $filter, first: $first, offset: $offset, orderBy: $orderBy) {
...post ...post
...postCounts ...postCounts
...tagsCategoriesAndPinned
author {
...user
...userCounts
...locationAndBadges
}
} }
} }
` `
@ -40,9 +71,12 @@ export const filterPosts = i18n => {
export const profilePagePosts = i18n => { export const profilePagePosts = i18n => {
const lang = i18n.locale().toUpperCase() const lang = i18n.locale().toUpperCase()
return gql` return gql`
${userFragment(lang)} ${userFragment}
${postFragment(lang)} ${userCountsFragment}
${locationAndBadgesFragment(lang)}
${postFragment}
${postCountsFragment} ${postCountsFragment}
${tagsCategoriesAndPinnedFragment}
query profilePagePosts( query profilePagePosts(
$filter: _PostFilter $filter: _PostFilter
@ -53,6 +87,12 @@ export const profilePagePosts = i18n => {
profilePagePosts(filter: $filter, first: $first, offset: $offset, orderBy: $orderBy) { profilePagePosts(filter: $filter, first: $first, offset: $offset, orderBy: $orderBy) {
...post ...post
...postCounts ...postCounts
...tagsCategoriesAndPinned
author {
...user
...userCounts
...locationAndBadges
}
} }
} }
` `
@ -69,17 +109,32 @@ export const PostsEmotionsByCurrentUser = () => {
export const relatedContributions = i18n => { export const relatedContributions = i18n => {
const lang = i18n.locale().toUpperCase() const lang = i18n.locale().toUpperCase()
return gql` return gql`
${userFragment(lang)} ${userFragment}
${postFragment(lang)} ${userCountsFragment}
${locationAndBadgesFragment(lang)}
${postFragment}
${postCountsFragment} ${postCountsFragment}
${tagsCategoriesAndPinnedFragment}
query Post($slug: String!) { query Post($slug: String!) {
Post(slug: $slug) { Post(slug: $slug) {
...post ...post
...postCounts ...postCounts
...tagsCategoriesAndPinned
author {
...user
...userCounts
...locationAndBadges
}
relatedContributions(first: 2) { relatedContributions(first: 2) {
...post ...post
...postCounts ...postCounts
...tagsCategoriesAndPinned
author {
...user
...userCounts
...locationAndBadges
}
} }
} }
} }

View File

@ -1,27 +1,38 @@
import gql from 'graphql-tag' import gql from 'graphql-tag'
import { linkableUserFragment, userFragment, postFragment, commentFragment } from './Fragments' import {
userCountsFragment,
locationAndBadgesFragment,
userFragment,
postFragment,
commentFragment,
} from './Fragments'
export default i18n => { export default i18n => {
const lang = i18n.locale().toUpperCase() const lang = i18n.locale().toUpperCase()
return gql` return gql`
${userFragment(lang)} ${userFragment}
${userCountsFragment}
${locationAndBadgesFragment(lang)}
query User($id: ID!) { query User($id: ID!) {
User(id: $id) { User(id: $id) {
...user ...user
...userCounts
...locationAndBadges
about about
locationName locationName
createdAt createdAt
badgesCount
followingCount
following(first: 7) {
...user
}
followedByCount
followedByCurrentUser followedByCurrentUser
isBlocked isBlocked
following(first: 7) {
...user
...userCounts
...locationAndBadges
}
followedBy(first: 7) { followedBy(first: 7) {
...user ...user
...userCounts
...locationAndBadges
} }
socialMedia { socialMedia {
id id
@ -47,11 +58,10 @@ export const minimisedUserQuery = () => {
} }
export const notificationQuery = i18n => { export const notificationQuery = i18n => {
const lang = i18n.locale().toUpperCase()
return gql` return gql`
${linkableUserFragment()} ${userFragment}
${commentFragment(lang)} ${commentFragment}
${postFragment(lang)} ${postFragment}
query($read: Boolean, $orderBy: NotificationOrdering, $first: Int, $offset: Int) { query($read: Boolean, $orderBy: NotificationOrdering, $first: Int, $offset: Int) {
notifications(read: $read, orderBy: $orderBy, first: $first, offset: $offset) { notifications(read: $read, orderBy: $orderBy, first: $first, offset: $offset) {
@ -63,11 +73,20 @@ export const notificationQuery = i18n => {
__typename __typename
... on Post { ... on Post {
...post ...post
author {
...user
}
} }
... on Comment { ... on Comment {
...comment ...comment
author {
...user
}
post { post {
...post ...post
author {
...user
}
} }
} }
} }
@ -77,11 +96,10 @@ export const notificationQuery = i18n => {
} }
export const markAsReadMutation = i18n => { export const markAsReadMutation = i18n => {
const lang = i18n.locale().toUpperCase()
return gql` return gql`
${linkableUserFragment()} ${userFragment}
${commentFragment(lang)} ${commentFragment}
${postFragment(lang)} ${postFragment}
mutation($id: ID!) { mutation($id: ID!) {
markAsRead(id: $id) { markAsRead(id: $id) {
@ -93,11 +111,17 @@ export const markAsReadMutation = i18n => {
__typename __typename
... on Post { ... on Post {
...post ...post
author {
...user
}
} }
... on Comment { ... on Comment {
...comment ...comment
post { post {
...post ...post
author {
...user
}
} }
} }
} }
@ -107,16 +131,19 @@ export const markAsReadMutation = i18n => {
} }
export const followUserMutation = i18n => { export const followUserMutation = i18n => {
const lang = i18n.locale().toUpperCase()
return gql` return gql`
${userFragment(lang)} ${userFragment}
${userCountsFragment}
mutation($id: ID!) { mutation($id: ID!) {
followUser(id: $id) { followUser(id: $id) {
name ...user
...userCounts
followedByCount followedByCount
followedByCurrentUser followedByCurrentUser
followedBy(first: 7) { followedBy(first: 7) {
...user ...user
...userCounts
} }
} }
} }
@ -124,16 +151,19 @@ export const followUserMutation = i18n => {
} }
export const unfollowUserMutation = i18n => { export const unfollowUserMutation = i18n => {
const lang = i18n.locale().toUpperCase()
return gql` return gql`
${userFragment(lang)} ${userFragment}
${userCountsFragment}
mutation($id: ID!) { mutation($id: ID!) {
unfollowUser(id: $id) { unfollowUser(id: $id) {
name ...user
...userCounts
followedByCount followedByCount
followedByCurrentUser followedByCurrentUser
followedBy(first: 7) { followedBy(first: 7) {
...user ...user
...userCounts
} }
} }
} }