GraphQL Internationalization demo

Just a way to explore what a schema of translated content could look like.

Playground: https://lang-demo.good-idea.now.sh/

Note: This implementation could be a start but is a very rough draft. For instance, it mutates the GraphQL context in a nasty way as different languages are queried.

Features:

Generate translated fields based on available translations.

type Article {
	author: Author!
	title: String!
}

becomes πŸ‘‡

type Article {
	author: Author!
	title: String! # πŸ‘‰ Falls back to current language
	title_en: String
	title_es: String
	title_translations: [String]! # πŸ‘‰ '['en', 'es']
}

Provide a lang: 'es' argument on a scalar to set the language or all child nodes

query {
   Article(lang: 'es') {
      title	# πŸ‘‰ 'CΓ³mo localicΓ© mi esquema GraphQL'
      author {
         bio # πŸ‘‰ 'Joseph vive en Los Angeles'
      }
   }
}

No language (falls back to defaults):

query {
	Article {
		title # πŸ‘‰ 'How I localized my GraphQL Schema'
		author {
			bio # πŸ‘‰ 'Joseph lives in Los Angeles'
		}
	}
}

Fields fall back to default (or parent) language

query {
   Article(lang: 'de') {
      title: # πŸ‘‰ 'How I localized my GraphQL Schema'
   }
}

Or, Provide a strict: true argument on a scalar to prevent resolving to the default translation:

query {
   Article(lang: 'de', strict: true) {
      title:  # πŸ‘‰ null
   }
}

A full query that demonstrates all of the above:

{
	article(id: 1) {
		title

		# Available translations
		title_translations

		# Specific translations
		title_en
		title_es
		title_de

		author {
			name
			bio
		}

		furtherReading {
			title
			url
		}

		# Querying for a different language on scalars
		furtherReadingES: furtherReading(lang: "es", strict: true) {
			title
			url
		}
	}

	articleES: article(id: 1, lang: "es") {
		title

		# Uses parent language ("es")
		author {
			name
			bio
		}

		# Uses parent language ("es")
		furtherReading {
			title
			url
		}
	}

	# Non-strict, title will return with English translation
	articleDE: article(id: 1, lang: "de") {
		title
	}

	# Strict, title will return `null`
	articleDEStrict: article(id: 1, lang: "de", strict: true) {
		title
	}
}