|
| 1 | +# Url — GraphQL Custom Scalar |
| 2 | + |
| 3 | +Author – ChilliCream |
| 4 | + |
| 5 | +Date – 2026-01-05 |
| 6 | + |
| 7 | +**License and Copyright** |
| 8 | + |
| 9 | +Copyright © GraphQL contributors. This specification is licensed under |
| 10 | +[OWFa 1.0](https://www.openwebfoundation.org/the-agreements/the-owf-1-0-agreements-granted-claims/owfa-1-0). |
| 11 | + |
| 12 | +# Overview |
| 13 | + |
| 14 | +The `Url` scalar type represents a Uniform Resource Locator (URL) as defined by |
| 15 | +RFC 3986. It is intended for scenarios where a field must contain a valid URL, |
| 16 | +such as links to external resources, API endpoints, image sources, or any |
| 17 | +web-accessible resource. |
| 18 | + |
| 19 | +Unlike the built-in `String` scalar which accepts any text, `Url` provides |
| 20 | +validation to ensure the value conforms to the URL specification. |
| 21 | + |
| 22 | +# Recommended name |
| 23 | + |
| 24 | +The recommended name for this scalar is `Url`. |
| 25 | + |
| 26 | +# Result spec |
| 27 | + |
| 28 | +A `Url` scalar must serialize to a string representation of a valid URL |
| 29 | +conforming to RFC 3986. |
| 30 | + |
| 31 | +A valid URL must include: |
| 32 | + |
| 33 | +- A scheme (e.g., `http`, `https`, `ftp`) |
| 34 | +- A hierarchical part (authority and/or path) |
| 35 | + |
| 36 | +The URL may optionally include: |
| 37 | + |
| 38 | +- A query string |
| 39 | +- A fragment identifier |
| 40 | + |
| 41 | +## Examples |
| 42 | + |
| 43 | +These are valid result values: |
| 44 | + |
| 45 | +| Value | Explanation | |
| 46 | +| ------------------------------------------ | ------------------------------- | |
| 47 | +| `"https://example.com"` | Simple HTTPS URL. | |
| 48 | +| `"https://example.com/path/to/resource"` | URL with path. | |
| 49 | +| `"https://example.com:8080/api?key=value"` | URL with port and query string. | |
| 50 | +| `"ftp://files.example.com/document.pdf"` | FTP URL. | |
| 51 | +| `"https://example.com/page#section"` | URL with fragment. | |
| 52 | + |
| 53 | +These are invalid result values: |
| 54 | + |
| 55 | +| Value | Why is it invalid | |
| 56 | +| ----------------- | ---------------------------------- | |
| 57 | +| `"not a url"` | Missing scheme and invalid format. | |
| 58 | +| `"//example.com"` | Missing scheme. | |
| 59 | +| `"http://"` | Missing authority/path. | |
| 60 | +| `"example.com"` | Missing scheme. | |
| 61 | +| `123` | Must be a string, not a number. | |
| 62 | + |
| 63 | +# Input spec |
| 64 | + |
| 65 | +A `Url` scalar accepts string values representing valid URLs conforming to RFC |
| 66 | +3986, both as GraphQL literals and as JSON input values. |
| 67 | + |
| 68 | +Implementations should validate: |
| 69 | + |
| 70 | +- String is a valid URL per RFC 3986 |
| 71 | +- URL includes a scheme |
| 72 | +- URL has a valid hierarchical structure |
| 73 | + |
| 74 | +## Examples |
| 75 | + |
| 76 | +Valid input values: |
| 77 | + |
| 78 | +GraphQL Literal: |
| 79 | + |
| 80 | +```graphql |
| 81 | +mutation { |
| 82 | + addBookmark(url: "https://example.com/article") { |
| 83 | + id |
| 84 | + } |
| 85 | +} |
| 86 | +``` |
| 87 | + |
| 88 | +JSON input: |
| 89 | + |
| 90 | +```json |
| 91 | +{ |
| 92 | + "url": "https://example.com/article" |
| 93 | +} |
| 94 | +``` |
| 95 | + |
| 96 | +```json |
| 97 | +{ |
| 98 | + "imageSource": "https://cdn.example.com/images/photo.jpg" |
| 99 | +} |
| 100 | +``` |
| 101 | + |
| 102 | +Invalid input values: |
| 103 | + |
| 104 | +| Value | Why is it invalid | |
| 105 | +| ----------------- | ---------------------------------- | |
| 106 | +| `"not a url"` | Missing scheme and invalid format. | |
| 107 | +| `"//example.com"` | Missing scheme. | |
| 108 | +| `"example.com"` | Missing scheme. | |
| 109 | +| `"http://"` | Missing authority/path. | |
| 110 | +| `""` | Empty string. | |
| 111 | + |
| 112 | +# References |
| 113 | + |
| 114 | +- [RFC 3986](https://www.rfc-editor.org/rfc/rfc3986) — Uniform Resource |
| 115 | + Identifier (URI): Generic Syntax |
0 commit comments