|
| 1 | +schema |
| 2 | + # Load the persisted operations document that contains the WeatherForecast operation |
| 3 | + @sdl( |
| 4 | + files: [] |
| 5 | + executables: [{ document: "operations.graphql", persist: true }] |
| 6 | + ) |
| 7 | + # Define a prescribed tool that maps to the WeatherForecast operation in the persisted document |
| 8 | + @tool(name: "weather-lookup", prescribed: "WeatherForecast") { |
| 9 | + query: Query |
| 10 | +} |
| 11 | + |
| 12 | +type Query { |
| 13 | + # Get weather forecast for a specific city |
| 14 | + weatherForecast(city: String!, days: Int = 1): WeatherForecast |
| 15 | + @value( |
| 16 | + script: { |
| 17 | + src: """ |
| 18 | + function weatherForecast() { |
| 19 | + const cities = { |
| 20 | + "new york": { |
| 21 | + name: "New York", |
| 22 | + country: "United States", |
| 23 | + latitude: 40.7128, |
| 24 | + longitude: -74.0060, |
| 25 | + timezone: "America/New_York", |
| 26 | + population: 8804190 |
| 27 | + }, |
| 28 | + "london": { |
| 29 | + name: "London", |
| 30 | + country: "United Kingdom", |
| 31 | + latitude: 51.5074, |
| 32 | + longitude: -0.1278, |
| 33 | + timezone: "Europe/London", |
| 34 | + population: 8982000 |
| 35 | + }, |
| 36 | + "tokyo": { |
| 37 | + name: "Tokyo", |
| 38 | + country: "Japan", |
| 39 | + latitude: 35.6762, |
| 40 | + longitude: 139.6503, |
| 41 | + timezone: "Asia/Tokyo", |
| 42 | + population: 13960000 |
| 43 | + }, |
| 44 | + "sydney": { |
| 45 | + name: "Sydney", |
| 46 | + country: "Australia", |
| 47 | + latitude: -33.8688, |
| 48 | + longitude: 151.2093, |
| 49 | + timezone: "Australia/Sydney", |
| 50 | + population: 5312000 |
| 51 | + } |
| 52 | + }; |
| 53 | +
|
| 54 | + // Default to 1 day if not specified or out of range |
| 55 | + const daysToForecast = days < 1 || days > 7 ? 1 : days; |
| 56 | +
|
| 57 | + const normalizedCity = city.toLowerCase(); |
| 58 | + const cityData = cities[normalizedCity] || { |
| 59 | + name: city, |
| 60 | + country: "Unknown", |
| 61 | + latitude: 0, |
| 62 | + longitude: 0, |
| 63 | + timezone: "UTC", |
| 64 | + population: null |
| 65 | + }; |
| 66 | +
|
| 67 | + // Fixed forecast patterns for each city |
| 68 | + const forecastPatterns = { |
| 69 | + "new york": [ |
| 70 | + { conditions: "Partly Cloudy", high: 22, low: 15, precipitation: 20, humidity: 65, windSpeed: 12, windDirection: "NW" }, |
| 71 | + { conditions: "Sunny", high: 24, low: 16, precipitation: 5, humidity: 55, windSpeed: 8, windDirection: "W" }, |
| 72 | + { conditions: "Cloudy", high: 20, low: 14, precipitation: 30, humidity: 70, windSpeed: 15, windDirection: "NE" }, |
| 73 | + { conditions: "Rain", high: 18, low: 12, precipitation: 75, humidity: 85, windSpeed: 18, windDirection: "E" }, |
| 74 | + { conditions: "Partly Cloudy", high: 23, low: 16, precipitation: 15, humidity: 60, windSpeed: 10, windDirection: "SW" }, |
| 75 | + { conditions: "Sunny", high: 25, low: 17, precipitation: 0, humidity: 50, windSpeed: 7, windDirection: "W" }, |
| 76 | + { conditions: "Thunderstorm", high: 21, low: 15, precipitation: 90, humidity: 90, windSpeed: 25, windDirection: "S" } |
| 77 | + ], |
| 78 | + "london": [ |
| 79 | + { conditions: "Cloudy", high: 16, low: 10, precipitation: 40, humidity: 75, windSpeed: 14, windDirection: "SW" }, |
| 80 | + { conditions: "Rain", high: 15, low: 9, precipitation: 65, humidity: 80, windSpeed: 16, windDirection: "W" }, |
| 81 | + { conditions: "Partly Cloudy", high: 17, low: 11, precipitation: 25, humidity: 70, windSpeed: 12, windDirection: "NW" }, |
| 82 | + { conditions: "Cloudy", high: 16, low: 10, precipitation: 35, humidity: 75, windSpeed: 13, windDirection: "SW" }, |
| 83 | + { conditions: "Rain", high: 14, low: 8, precipitation: 70, humidity: 85, windSpeed: 18, windDirection: "W" }, |
| 84 | + { conditions: "Partly Cloudy", high: 18, low: 12, precipitation: 20, humidity: 65, windSpeed: 11, windDirection: "NW" }, |
| 85 | + { conditions: "Sunny", high: 19, low: 13, precipitation: 10, humidity: 60, windSpeed: 9, windDirection: "N" } |
| 86 | + ], |
| 87 | + "tokyo": [ |
| 88 | + { conditions: "Sunny", high: 26, low: 19, precipitation: 5, humidity: 60, windSpeed: 10, windDirection: "E" }, |
| 89 | + { conditions: "Partly Cloudy", high: 25, low: 18, precipitation: 15, humidity: 65, windSpeed: 12, windDirection: "SE" }, |
| 90 | + { conditions: "Cloudy", high: 23, low: 17, precipitation: 30, humidity: 70, windSpeed: 14, windDirection: "S" }, |
| 91 | + { conditions: "Rain", high: 22, low: 16, precipitation: 60, humidity: 80, windSpeed: 16, windDirection: "SW" }, |
| 92 | + { conditions: "Partly Cloudy", high: 24, low: 18, precipitation: 20, humidity: 65, windSpeed: 11, windDirection: "E" }, |
| 93 | + { conditions: "Sunny", high: 27, low: 20, precipitation: 5, humidity: 55, windSpeed: 9, windDirection: "NE" }, |
| 94 | + { conditions: "Cloudy", high: 24, low: 18, precipitation: 25, humidity: 68, windSpeed: 13, windDirection: "SE" } |
| 95 | + ], |
| 96 | + "sydney": [ |
| 97 | + { conditions: "Sunny", high: 28, low: 20, precipitation: 10, humidity: 60, windSpeed: 15, windDirection: "NE" }, |
| 98 | + { conditions: "Partly Cloudy", high: 27, low: 19, precipitation: 15, humidity: 65, windSpeed: 14, windDirection: "E" }, |
| 99 | + { conditions: "Sunny", high: 29, low: 21, precipitation: 5, humidity: 55, windSpeed: 13, windDirection: "NE" }, |
| 100 | + { conditions: "Partly Cloudy", high: 26, low: 19, precipitation: 20, humidity: 70, windSpeed: 16, windDirection: "SE" }, |
| 101 | + { conditions: "Cloudy", high: 24, low: 18, precipitation: 35, humidity: 75, windSpeed: 18, windDirection: "S" }, |
| 102 | + { conditions: "Rain", high: 22, low: 17, precipitation: 70, humidity: 85, windSpeed: 20, windDirection: "SW" }, |
| 103 | + { conditions: "Partly Cloudy", high: 25, low: 18, precipitation: 25, humidity: 70, windDirection: "W", windSpeed: 17 } |
| 104 | + ] |
| 105 | + }; |
| 106 | +
|
| 107 | + // Get pattern for city or use default |
| 108 | + const pattern = forecastPatterns[normalizedCity] || forecastPatterns["new york"]; |
| 109 | +
|
| 110 | + // Generate forecast for the requested number of days |
| 111 | + const forecast = []; |
| 112 | + const today = new Date(); |
| 113 | +
|
| 114 | + for (let i = 0; i < daysToForecast; i++) { |
| 115 | + const forecastDate = new Date(); |
| 116 | + forecastDate.setDate(today.getDate() + i); |
| 117 | +
|
| 118 | + const dayPattern = pattern[i % pattern.length]; |
| 119 | +
|
| 120 | + forecast.push({ |
| 121 | + date: forecastDate.toISOString().split('T')[0], |
| 122 | + sunrise: "06:25 AM", |
| 123 | + sunset: "06:35 PM", |
| 124 | + high: { |
| 125 | + celsius: dayPattern.high, |
| 126 | + fahrenheit: Math.round(dayPattern.high * 9 / 5 + 32) |
| 127 | + }, |
| 128 | + low: { |
| 129 | + celsius: dayPattern.low, |
| 130 | + fahrenheit: Math.round(dayPattern.low * 9 / 5 + 32) |
| 131 | + }, |
| 132 | + conditions: dayPattern.conditions, |
| 133 | + precipitation: dayPattern.precipitation, |
| 134 | + humidity: dayPattern.humidity, |
| 135 | + windSpeed: dayPattern.windSpeed, |
| 136 | + windDirection: dayPattern.windDirection |
| 137 | + }); |
| 138 | + } |
| 139 | +
|
| 140 | + return { |
| 141 | + city: cityData, |
| 142 | + forecast: forecast, |
| 143 | + }; |
| 144 | + } |
| 145 | + weatherForecast() |
| 146 | + """ |
| 147 | + } |
| 148 | + ) |
| 149 | +} |
| 150 | + |
| 151 | +type WeatherForecast { |
| 152 | + city: City! |
| 153 | + forecast: [DailyForecast!]! |
| 154 | + lastUpdated: DateTime |
| 155 | +} |
| 156 | + |
| 157 | +type City { |
| 158 | + name: String! |
| 159 | + country: String! |
| 160 | + latitude: Float! |
| 161 | + longitude: Float! |
| 162 | + timezone: String! |
| 163 | + population: Int |
| 164 | +} |
| 165 | + |
| 166 | +type DailyForecast { |
| 167 | + date: Date! |
| 168 | + sunrise: String! |
| 169 | + sunset: String! |
| 170 | + high: Temperature! |
| 171 | + low: Temperature! |
| 172 | + conditions: String! |
| 173 | + precipitation: Float! |
| 174 | + humidity: Int! |
| 175 | + windSpeed: Float! |
| 176 | + windDirection: String! |
| 177 | +} |
| 178 | + |
| 179 | +type Temperature { |
| 180 | + celsius: Float! |
| 181 | + fahrenheit: Float! |
| 182 | +} |
| 183 | + |
| 184 | +scalar Date |
| 185 | +scalar DateTime |
0 commit comments